gpt4 book ai didi

java - JUnit 5 测试

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:53 24 4
gpt4 key购买 nike

如何使用 java 和 @test 注释通过 junit 测试用例实现此类中 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() 方法实现了这一点,尽管我正在努力为其他两种方法编写测试用例,因为它们都返回对象指针,并且我不知道如何在我的测试文件中初始化它。

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

最佳答案

也许低于测试,同时有助于获得全面覆盖。

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 是您测试的类的对象。

关于java - JUnit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com