为测试目的创建许多对象
Create Many Object for Testing Purposes
我想知道对您而言,创建大量对象的最佳方式是什么。
我举个例子。
我想模拟 50 种产品,产品是 class,字段名称为字符串,价格为整数。
public class StuffTO {
String category;
String name;
String price;
String qty;
}
我不需要特定的名称或价格,它们可以是随机的。
现在我想知道哪种是创建 50 个随机产品的最佳方法,编写的代码可能更少。
public List<StuffTO> create(final int amount){
final Random random = new Random();
final List<StuffTO> list = new ArrayList<>();
for(int i=0;i<amount;i++){
list.add(new StuffTO("cat"+random.nextInt(100), "name"+random.nextInt(100), "price"+random.nextInt(100), "qty"+random.nextInt(100)));
}
return list;
}
class StuffTO {
String category;
String name;
String price;
String qty;
public StuffTO(){
}
public StuffTO(String category, String name, String price, String qty) {
this.category = category;
this.name = name;
this.price = price;
this.qty = qty;
}
}
QuickCheck 有一个 Java 实现,它具有用于生成测试数据的 API。
Basically QuickCheck is about generators of data. The QuickCheck runner method is just a fancy for loop implementation. QuickCheck can help in scenarios where whole classes of test cases have to be tested and it is not feasible to write tests for all distinct test scenarios.
github and published to maven central 上还有 JFixture。
这仍在积极开发中,功能请求正在得到满足。
JFixture is a Java library to assist in the writing of Unit Tests, particularly when following Test Driven Development. It generates types based on the concept of 'constrained non-determinism', which is an implementation of the Generated Value xUnit test pattern.
List<StuffTo> stuff = new ArrayList<>;
List<String> categories = {"category1", "category2", ....};
List<String> names = {"name1", "name2", ....};
List<String> prices = {"price1", "price2", ....};
List<String> qtys= {"qty1", "qty2", ....};
for(int i = 0; i <= 50; i++){
StuffTo stuffTo = new StuffTo;
stuffTo.category = catefories[i];
stuffTo.name = names[i];
stuffTo.price = prices[i];
stuffTo.qty = qtys.[i];
stuff.Add(stuffTo);
}
这只是一个基本想法。现在你只需要遍历这个东西列表。希望对你有帮助。
-法比安
您可以在循环中创建随机对象(例如代码中的 50)并将它们存储在任何集合中(例如代码中的 Arraylist)。该代码将 运行 用于您提供的限制并创建对象并将它们存储在 Arraylist 中。
int i=0;
ArrayList<StuffTO > TestList= new ArrayList<StuffTO >();
while(i<50) {
TestList.add(new StuffTO ( "aa"+i, "bb"+i, "cc"+i, "dd"+i));
i++;
}
我想知道对您而言,创建大量对象的最佳方式是什么。
我举个例子。 我想模拟 50 种产品,产品是 class,字段名称为字符串,价格为整数。
public class StuffTO {
String category;
String name;
String price;
String qty;
}
我不需要特定的名称或价格,它们可以是随机的。
现在我想知道哪种是创建 50 个随机产品的最佳方法,编写的代码可能更少。
public List<StuffTO> create(final int amount){
final Random random = new Random();
final List<StuffTO> list = new ArrayList<>();
for(int i=0;i<amount;i++){
list.add(new StuffTO("cat"+random.nextInt(100), "name"+random.nextInt(100), "price"+random.nextInt(100), "qty"+random.nextInt(100)));
}
return list;
}
class StuffTO {
String category;
String name;
String price;
String qty;
public StuffTO(){
}
public StuffTO(String category, String name, String price, String qty) {
this.category = category;
this.name = name;
this.price = price;
this.qty = qty;
}
}
QuickCheck 有一个 Java 实现,它具有用于生成测试数据的 API。
Basically QuickCheck is about generators of data. The QuickCheck runner method is just a fancy for loop implementation. QuickCheck can help in scenarios where whole classes of test cases have to be tested and it is not feasible to write tests for all distinct test scenarios.
github and published to maven central 上还有 JFixture。 这仍在积极开发中,功能请求正在得到满足。
JFixture is a Java library to assist in the writing of Unit Tests, particularly when following Test Driven Development. It generates types based on the concept of 'constrained non-determinism', which is an implementation of the Generated Value xUnit test pattern.
List<StuffTo> stuff = new ArrayList<>;
List<String> categories = {"category1", "category2", ....};
List<String> names = {"name1", "name2", ....};
List<String> prices = {"price1", "price2", ....};
List<String> qtys= {"qty1", "qty2", ....};
for(int i = 0; i <= 50; i++){
StuffTo stuffTo = new StuffTo;
stuffTo.category = catefories[i];
stuffTo.name = names[i];
stuffTo.price = prices[i];
stuffTo.qty = qtys.[i];
stuff.Add(stuffTo);
}
这只是一个基本想法。现在你只需要遍历这个东西列表。希望对你有帮助。
-法比安
您可以在循环中创建随机对象(例如代码中的 50)并将它们存储在任何集合中(例如代码中的 Arraylist)。该代码将 运行 用于您提供的限制并创建对象并将它们存储在 Arraylist 中。
int i=0;
ArrayList<StuffTO > TestList= new ArrayList<StuffTO >();
while(i<50) {
TestList.add(new StuffTO ( "aa"+i, "bb"+i, "cc"+i, "dd"+i));
i++;
}