【问题标题】:JUnit Tests with classes that rely on other classes使用依赖于其他类的类进行 JUnit 测试
【发布时间】:2018-05-02 13:16:02
【问题描述】:

你好我有这个类项目

public class Item implements Cloneable {

    private String name;
    private int reorderAmount;


    public Item(String name, int reorderAmount) {
        this.name = name;
        this.reorderAmount = reorderAmount;

     }


   /**
     * @return The Amount of a reorder.
     */
    public int getReorderAmount() {
        return reorderAmount;
    }
}

我的另一门课是股票

public class Stock extends HashMap {

    private HashMap<String, Item> stock;

    /**
     * Constructor. Creates a stock. 
     */
    public Stock() {
        stock = new HashMap<>();
    }

    /**
     * Calculates the total Quantity of Items for the next Order.
     * @return Number of total reorder quantity.
     */
    public int getTotalReorderAmount() {
        int reorderQuantity = 0;
        for (Item item : (Collection<Item>) this.values()) {
            reorderQuantity += item.getReorderAmount();
        }
        return reorderQuantity;
    }
}

我在运行 JUnit 测试时遇到问题,因为我缺乏对一个类如何影响另一个类的理解。

public class StockTests  {

            Stock stock; 
            Item item; 


            // Clear the item and stock object before every test 
            @Before
            public void setUp() {
                String name = "bread";
                Integer reorderAmount = 100; 
                item = new Item(name, reorderAmount);

                stock = null;
            }

            /*
             * Test 1: Test the total number of items needed.
             */
            @Test
            public void testReorderAmount() {
                stock = new Stock();
                assertEquals(100, stock.getTotalReorderAmount());
            }

}

我目前所做的是在我的 Junit 测试类的@before 中创建一个项目“面包”,再订购量为 100。我正在测试我的 Stock 类中的方法 getTotalReorderAmount 是否返回 100,但我的 JUnit 结果告诉我它返回 0。这是我认为我在 JUnit 类中错误地创建项目的地方。

【问题讨论】:

  • @Test 中的 stock 是一个新实例,因此它不包含任何内容。
  • Stock 类中没有添加任何项目的方法,因此您永远无法向其中添加任何项目(因此您的测试按预期返回 0。)另外,为什么 @987654326 @扩展HashMap? (您的 setup() 方法确实创建了一个项目,但它不会添加到任何内容中。)

标签: java class junit


【解决方案1】:

您创建了一个项目,但从未将其添加到Stock

一个简单的实现:

 public class Stock {
     ....

     public void add(Item item) {
         stock.put(item.getName(), item);
     }
 }

您的测试可能是:

        /*
         * Test 1: Test the total number of items needed.
         */
        @Test
        public void testReorderAmount() {
            Stock stock = new Stock();
            stock.add(new Item("bread", 100));
            assertEquals(100, stock.getTotalReorderAmount());
        }

在这种情况下不需要使用setUp

顺便说一句,最基本的测试用例(建议从头开始)是空库存:

@Test
public void emptyStock() {
   Stock stock = new Stock(); 
   assertEquals(0, stock.getTotalReorderAmount()); 
}

【讨论】:

    【解决方案2】:

    在您的testReorderAmount 方法中,您必须设置您创建的item

    首先修改您的Stock 类,使其具有在private HashMap&lt;String, Item&gt; stock 中添加项目的方法。 即你的班级Stock 可能看起来像:

    public class Stock {
    
      .............
    
        private HashMap<String, Item> stock;
    
        public void addItemToStock(String itemName, Item item){
           stock.put(itemName, item);
        }
    
        /**
         * Constructor. Creates a stock. 
         */
        public Stock() {
            stock = new HashMap<>();
        }
     .........
    
    }
    

    其次,在您的 junit 测试中将 item 设置在 stock 映射中。

    您的测试方法将如下所示:

     /*
     * Test 1: Test the total number of items needed.
     */
    @Test
    public void testReorderAmount() {
        stock = new Stock();
        stock.addItem("bread", this.item);
        assertEquals(100, stock.getTotalReorderAmount());
    }
    

    【讨论】:

    • 在这个解决方案中,您在 stock 类 bad idea 之外泄漏了一个 mutable 映射。
    • 由于地图的曝光,我投了反对票,因为它违反了良好的设计。删除了downvote,现在答案与我的几乎相同。除了您将项目的name 传递给方法之外,这会带来风险,因为您可以传递项目所具有的另一个名称,并且它会“泄漏”一些内部数据结构作为映射。
    • 既然可以从传递的item中得到名字addItemToStock(String itemName, Item item),为什么还要传递呢?
    • 我们如何推断stockname是项目名称?它可以是其他名称,例如库存名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多