简体   繁体   English

JUnit - 使用参数中的对象测试类实例化

[英]JUnit - Test class instantiation with objects in parameters

I am very new to JUnit testing and I am trying to understand how to test the instantiation of an class. 我是JUnit测试的新手,我正在尝试了解如何测试类的实例化。

Let us say that I have the following ToyBox class which needs an ArrayList<Toy> in order to be instantiated. 让我们说我有以下ToyBox类需要一个ArrayList<Toy>才能实例化。 This list of toys is created on another part of the program, of course, but I do not understand very well where I should create it in order to unit test ToyBox . 当然,这个玩具列表是在程序的另一部分创建的,但我不太清楚我应该在哪里创建它以便对ToyBox进行单元测试。

ToyBox Class 玩具盒类

public ToyBox(ArrayList<Toy> toyList){
    this.toys= toyList;

    for (Toy toy: toyList) {
        checkToy(toy);
    }
}

private void checkToy(Toy toy){
    if (toy.isRed()){
        this.numRed += 1;
    } else {
        this.numBlue += 1;
    }
}
public int getBlues(){
    return this.numBlue;
}

ToyBoxTest ToyBoxTest

public class ToyBoxTest {

    @Test
    public void getNumBlues() throws Exception {
        // assert that num blues corresponds
    }

Where should I instantiate the ToyBox class in order to perform the getNumBlues() method? 我应该在哪里实例化ToyBox类以执行getNumBlues()方法?

Should it be like this? 应该是这样吗?

public class ToyBoxTest {
    ArrayList<Toy> toyList = new ArrayList<Toy>();
    Toy toy1 = new Toy("blue", "car");
    Toy toy2 = new Toy("red", "bike");
    toyList.add(toy1);
    toyList.add(toy2);

    @Test
    public void getNumBlues() throws Exception {
        // assert that num blues corresponds
        ToyBox box = new ToyBox(toyList);
        assertEquals(1, box.getBlues());
    }

Basically, my question is where and how should I create the arraylist of objects needed to test a class that depends on that created list. 基本上,我的问题是我应该在哪里以及如何创建测试依赖于创建列表的类所需的对象的arraylist。

Most tutorials will state that the best practice is to instantiate the object you're about to test in a setup method (a @Before method in JUnit's terminology). 大多数教程都会说最佳实践是在setup方法中实例化你要测试的对象(JUnit术语中的@Before方法)。 However, your usecase doesn't fit this pattern well. 但是,您的用例不适合这种模式。 As your constructor holds all the logic, you should instantiate the object in the test itself, and then assert that getNumBlues() and getNumReds() return the correct results. 当您的构造函数包含所有逻辑时,您应该在测试本身中实例化该对象,然后断言getNumBlues()getNumReds()返回正确的结果。 Eg: 例如:

@Test
public void bothColors() throws Exception {
    ArrayList<Toy> toyList = new ArrayList<>(Arrays.asList
        new Toy("blue", "car"),
        new Toy("red", "bike"));
    ToyBox box = new ToyBox(toyList);
    assertEquals(1, box.getBlues());
}

@Test
public void justBlues() throws Exception {
    ArrayList<Toy> toyList = new ArrayList<>(Arrays.asList
        new Toy("blue", "car"),
        new Toy("blue", "bike"));
    ToyBox box = new ToyBox(toyList);
    assertEquals(2, box.getBlues());
}

// etc...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM