JUnit使用依赖于其他类的类进行测试

时间:2018-05-02 13:16:03

标签: java class junit

嗨,我有这个课程项目

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());
            }

}

我目前所做的是创建一个项目面包&#39;在我的Junit测试类的@before里面,100作为重新订购金额。我正在测试我的Stock Class中的方法getTotalReorderAmount是否返回100但是我的JUnit结果告诉我它返回0.这是我相信我在JUnit类中错误地创建了Item。

2 个答案:

答案 0 :(得分:0)

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

首先修改您的Stock课程,以获得在private HashMap<String, Item> 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());
}

答案 1 :(得分:0)

您创建了一个项目,但从未将其添加到10, 20, 30, 40, 50, 60, 70, 80

简单的实施:

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