简体   繁体   中英

JUnit 5 testing

How do i achieve full branch coverage on the 3 methods in this class with junit test cases using java and the @test annotation.

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;

}

i have written the following test casses and i have achieved this for the isEmpty() method although i am struggling to write test cases for the other two methods because they both return object pointers and i do not know how to initialize that in my test file.

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

I will give you an example for the first function

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

You need 3 unity tests to fully cover this one

  1. For pointer == capacity
  2. One for pointer > capacity
  3. And the last one for pointer < capacity

For branch coverage you need only 1 and 3 or 2 and 3 though I would suggest all three for critical pieces of functionality.

Maybe below test while help to get full coverage.

push method test

  • pointer >= capacity

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

    @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

  • pointer < 0

     @Test(exception = IllegalArgumentException.class) public void pop_PointerNegative_ExceptionThrow(){ WhiteBox.setInternalState(yourObject, "pointer",-1); String inputString = "Hello"; yourObject.push(inputString); }
  • pointer > 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]); }

Here, yourObject is the object of class which one you test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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