【问题标题】:Create Many Object for Testing Purposes为测试目的创建许多对象
【发布时间】:2015-08-11 13:36:50
【问题描述】:

我想知道,对您来说,创建大量对象的最佳方式是什么。

我举个例子。 我想模拟 50 个产品,产品是一个具有字段 STRING 名称和 INTEGER 价格的类。

 public class StuffTO {

     String category;
     String name;
     String price;
     String qty;
 }

我不需要特定的名称或价格,它们可以是随机的。

现在我想知道用更少的代码创建 50 个随机产品的最佳方法是什么..

【问题讨论】:

  • 一个for循环和Random
  • 如果你想要“整数价格”,然后将String price 更改为int price(也许双倍会更好)。此外,qty 可能应该是一个 int 或一个 short。
  • 我在 Groovy 中为此编写了一个实用程序 - coffeaelectronica.com/blog/2015/property-randomization.html 我还没有针对 Java POJO 对其进行过测试,但它可能会为您所寻找的内容提供一些想法。

标签: java spring object testing mocking


【解决方案1】:

QuickCheck 有一个 Java 实现,它具有用于生成测试数据的 API。

基本上,QuickCheck 是关于数据生成器的。 QuickCheck runner 方法只是一个花哨的 for 循环实现。 QuickCheck 可以在需要测试整个类的测试用例并且无法为所有不同的测试场景编写测试的情况下提供帮助。

还有 JFixture,可在 github 上找到并发布到 maven central。 这仍在积极开发中,并且正在满足功能请求。

JFixture 是一个帮助编写单元测试的 Java 库,尤其是在遵循测试驱动开发时。它基于“受约束的非确定性”概念生成类型,这是 Generated Value xUnit 测试模式的一种实现。

【讨论】:

    【解决方案2】:

    您可以在循环中创建随机对象(例如代码中的 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++;
    }
    

    【讨论】:

    • 正确。添加所需的构造函数,只是为了获得更完整的答案,可以解决问题(即使它不是“随机的”,但我想这并不重要)。我也会使用for,但这是个人喜好问题 :)
    • 我想使用一些框架,这是因为我不想在我的项目中添加任何无用的实用程序类。我尝试使用 ekostadinov 建议的 JFixtture,它工作正常!而且它允许我定义一个值的范围......
    【解决方案3】:
    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;
        }
    
    }
    

    【讨论】:

    • 默认构造函数的作用是什么?
    • 习惯了,因为某些包(gson、jackson)需要默认构造函数来自动映射 pojo。为了安全起见。我不知道这个对象还需要做什么......
    【解决方案4】:
    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);
    }
    

    这只是一个基本想法。现在你只需要遍历材料列表。希望对您有所帮助。

    -法比安

    【讨论】:

    • 不是一个很好的方法。用户必须编写 200(或 204...)个字符串,而且它也没有编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多