我有这个if-else语句,我想编写一个J单元测试,该测试将测试每条线以查看是正确的。有什么想法吗?
public class Testings {
public static int computeValue(int x, int y, int z) {
int value = 0;
if ( x == y )
value = x + 1;
else if (( x > y) && ( z == 0))
value = y + 2;
else
value = z;
return value;
}
}
用涵盖所有情况的值测试您的方法:
- x == y
- x!= y和x> y和z == 0
- x!= y和x> y和z!= 0
- x!= y和x<y和z == 0
- x!= y和x<y和z!= 0
- x == y和z!= 0
- x == y和z == 0
示例:
assertEquals(4, Testings.computeValue(3, 3, 10)); // x == y
assertEquals(7, Testings.computeValue(10, 5, 0)); // x != y and x > y and z == 0
assertEquals(1, Testings.computeValue(10, 5, 1)); // x != y and x > y and z != 0
...
另外,您应该通过测试方法有一个 sustert ,以便给它一个专有名称:
@Test
public void testXEqualY(){
int x=3, y=3, z=10;
assertEquals(4, Testings.computeValue(x, y, z));
}