JUnit 5 测试

JUnit 5 testing

我如何使用 java 和 @test 注释实现此 class 中 3 种方法的完整分支覆盖。

public class StringStack {
private int capacity = 10;
private int pointer = 0;
private String[] objects = new String[capacity];

public void push(String o) {
    if (pointer >= capacity)
        throw new IllegalArgumentException("Stack exceeded capacity!");
    objects[pointer++] = o;

}
public String pop() {
    if (pointer <= 0)
        throw new IllegalArgumentException("Stack empty");
    return objects[--pointer];

}

public boolean isEmpty() {
    return pointer <= 0;

}

我已经编写了以下测试用例,并且我已经为 isEmpty() 方法实现了这一点,尽管我正在努力为其他两种方法编写测试用例,因为它们都是 return 对象指针,而我没有知道如何在我的测试文件中初始化它。

class squareTest {

    //1.
    @Test 
    public void push() {

        StringStack push1 = new StringStack();
        String e2 = push1.pop();
        try {
            Assert.fail( "Should have thrown an exception" );
        assertEquals(IllegalArgumentException("Stack empty"), e2);
        //java.lang.IllegalArgumentException: Stack empty

        }catch (Exception e) {
            String expmessage = "I should get this message";


        }




    }

    @Test
    public void testTC3()
    {
        try {
            StringStack.push(o);
            fail(); // if we got here, no exception was thrown, which is bad
        } 
        catch (Exception e) {
            final String expected = "Legal Values: Package Type must be P or R";
            assertEquals( expected, e.getMessage());
        }        
    }

    //3.EMPTY TEST CASES
    @Test
    public void empty()
    {
        StringStack test2 = new StringStack();
        boolean e1 = test2.isEmpty();
        assertEquals(true, e1);
    } 
    @Test
    public void notEmpty()
    {
        StringStack test3 = new StringStack();
        boolean ne1 = test3.equals("im not empty");
        assertEquals(false, ne1);
    }
}

第一个函数我给大家举个例子

public void push(String o) {
    if (pointer >= capacity)
        throw new IllegalArgumentException("Stack exceeded capacity!");
    objects[pointer++] = o;
}

你需要 3 个统一测试来完全覆盖这个

  1. 对于指针==容量
  2. 一个指针 > 容量
  3. 最后一个指针<容量

对于分支覆盖,您只需要 1 和 3 或 2 和 3,但我建议所有三个用于关键功能。

可能会在下面进行测试,同时有助于获得全面覆盖。

push method test

  • 指针 >= 容量

    @Test(exception = IllegalArgumentException.class)
    public void push_PointerGreaterThanCapacity_ExceptionThrow(){
        WhiteBox.setInternalState(yourObject, "pointer",11);
        String inputString  = "Hello";
        yourObject.push(inputString);
    }
    
  • 指针<容量

    @Test
    public void push_PointerSmallerThanCapacity_ExceptionThrow(){
        String inputString  = "Hello";
        yourObject.push(inputString);
        int pointer = WhiteBox.getInternalState(yourObject,"pointer");
        String[] objects = WhiteBox.getInternalState(yourObject,"objects");
    
        assertEquals(inputString, objects[pointer-1]);
    }
    

pop method test

  • 指针 < 0

    @Test(exception = IllegalArgumentException.class)
    public void pop_PointerNegative_ExceptionThrow(){
        WhiteBox.setInternalState(yourObject, "pointer",-1);
        String inputString  = "Hello";
        yourObject.push(inputString);
    }
    
  • 指针 > 0

    @Test
    public void pop_pointerGreaterThenZero_PopValue(){
        //set pointer
        WhiteBox.setInternalState(yourObject, "pointer",2);
        String[] stringList = {"String0","String1","String2"};
        //object array
        WhiteBox.setInternalState(yourObject, "objects",stringList);
    
        String actualOutput = yourObject.pop();
        assertEquals(actualOutput, stringList[1]);
    }
    

这里yourObject是class你测试的对象