使用 Junit 的 TriangleTest:不允许使用 void 类型
TriangleTest using Junit: void type is not allowed
我有这段代码,我尝试使用 Junit 创建单元测试。当我尝试如下测试 MAIN 时;
@Test
public void testMain(){
assertEquals(determineType(0,1,1),"ABCDEF");
}
它一直告诉我这里不允许使用void类型。我必须做什么?
package triangle;
public class Triangle {
public static void determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
System.out.println("Not a Triangle");
} else if (a == b && b == c) {
System.out.println("Equilateral Triangle");
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
System.out.println("Right Triangle");
} else if (a != b && b != c && c != a) {
System.out.println("Scalene Triangle");
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
System.out.println("Isosceles Triangle");
}
}
public static void main(String[] args) {
determineType(1, 1, 9);
}
}
可以测试不 return 任何东西的方法(无效),但您必须测试该方法的副作用,但在您的情况下 determineType
方法没有显示任何副作用-效果,它更像是一种检查方法,而不是 returning 任何东西,即 void
,并且由于它没有 returning 任何东西,所以你不能使用 assertEquals(expected,actual)
,因为 assertEquals
始终是 操作的实际结果与预期结果 .
之间的比较
因此,为了测试此方法,您可以将 determineType
方法的 return 类型从 void
更改为 String
(即删除 sysouts
并且 return
每个比较条件的结果 r\e.g. "Not a Triangle" 等)。
然后 5
个不同的测试用例来检查您的 determineType
方法中指定的每个条件,并将 returned String
与您的预期输出进行比较。
有点像,
@Test
public void testNotATriangle(){
assertEquals(determineType(0,1,1), "Not a Triangle");
}
@Test
public void testEquilateralTriangle(){
assertEquals(determineType(1,1,1), "Equilateral Triangle");
}
// other test case here, for each condition
public static String determineType(int a, int b, int c) {
if(a >= (b+c) || c >= (b+a) || b >= (a+c) ) {
return "Not a Triangle";
} else if(a==b && b==c) {
return "Equilateral Triangle";
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b)|| ((c * c) + (b * b)) == (a * a)) {
return "Right Triangle";
} else if(a!=b && b!=c && c!=a) {
return "Scalene Triangle";
} else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
return "Isosceles Triangle";
}
return null;
}
public static void main(String [] args) {
determineType(1,1,9);
}
如果我是你,我会稍微改变一下方法,而不是打印出来,我会 return 一个表示 Triangle 类型的枚举。在其他地方测试和实际使用该方法会容易得多。
public class Triangle {
public static TriangleType determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
return TriangleType.NONE;
} else if (a == b && b == c) {
return TriangleType.EQUILATERAL;
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
return TriangleType.RIGHT;
} else if (a != b && b != c && c != a) {
return TriangleType.SCALENE;
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
return TriangleType.ISOSCELES;
}
return TriangleType.NONE;
}
public static void main(String[] args) {
final TriangleType type = determineType(1, 1, 9);
System.out.println(type);
}
enum TriangleType {
EQUILATERAL, RIGHT, SCALENE, ISOSCELES, NONE
}
}
测试如下:
@Test
public void determineType() {
Assertions.assertAll(
() -> assertEquals("Should be isosceles", Triangle.TriangleType.ISOSCELES, Triangle.determineType(2, 2, 3)),
() -> assertEquals("Should be equilateral", Triangle.TriangleType.EQUILATERAL, Triangle.determineType(2, 2, 2)),
() -> assertEquals("Should be right", Triangle.TriangleType.RIGHT, Triangle.determineType(4, 3, 5))
);
}
试试这个代码,我已经修复了错误
测试代码
@Test
public void testMain(){
assertEquals(determineType(0,1,1),"Not a Triangle");
}
源代码
package triangle;
public class Triangle {
public static String determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
return "Not a Triangle";
} else if (a == b && b == c) {
return "Equilateral Triangle";
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
return "Right Triangle";
} else if (a != b && b != c && c != a) {
return "Scalene Triangle";
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
return "Isosceles Triangle";
}
// other case
return null;
}
public static void main(String[] args) {
System.out.println(determineType(1, 1, 9));
}
}
我有这段代码,我尝试使用 Junit 创建单元测试。当我尝试如下测试 MAIN 时;
@Test
public void testMain(){
assertEquals(determineType(0,1,1),"ABCDEF");
}
它一直告诉我这里不允许使用void类型。我必须做什么?
package triangle;
public class Triangle {
public static void determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
System.out.println("Not a Triangle");
} else if (a == b && b == c) {
System.out.println("Equilateral Triangle");
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
System.out.println("Right Triangle");
} else if (a != b && b != c && c != a) {
System.out.println("Scalene Triangle");
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
System.out.println("Isosceles Triangle");
}
}
public static void main(String[] args) {
determineType(1, 1, 9);
}
}
可以测试不 return 任何东西的方法(无效),但您必须测试该方法的副作用,但在您的情况下 determineType
方法没有显示任何副作用-效果,它更像是一种检查方法,而不是 returning 任何东西,即 void
,并且由于它没有 returning 任何东西,所以你不能使用 assertEquals(expected,actual)
,因为 assertEquals
始终是 操作的实际结果与预期结果 .
因此,为了测试此方法,您可以将 determineType
方法的 return 类型从 void
更改为 String
(即删除 sysouts
并且 return
每个比较条件的结果 r\e.g. "Not a Triangle" 等)。
然后 5
个不同的测试用例来检查您的 determineType
方法中指定的每个条件,并将 returned String
与您的预期输出进行比较。
有点像,
@Test
public void testNotATriangle(){
assertEquals(determineType(0,1,1), "Not a Triangle");
}
@Test
public void testEquilateralTriangle(){
assertEquals(determineType(1,1,1), "Equilateral Triangle");
}
// other test case here, for each condition
public static String determineType(int a, int b, int c) {
if(a >= (b+c) || c >= (b+a) || b >= (a+c) ) {
return "Not a Triangle";
} else if(a==b && b==c) {
return "Equilateral Triangle";
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b)|| ((c * c) + (b * b)) == (a * a)) {
return "Right Triangle";
} else if(a!=b && b!=c && c!=a) {
return "Scalene Triangle";
} else if ((a==b && b!=c ) || (a!=b && c==a) || (c==b && c!=a)) {
return "Isosceles Triangle";
}
return null;
}
public static void main(String [] args) {
determineType(1,1,9);
}
如果我是你,我会稍微改变一下方法,而不是打印出来,我会 return 一个表示 Triangle 类型的枚举。在其他地方测试和实际使用该方法会容易得多。
public class Triangle {
public static TriangleType determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
return TriangleType.NONE;
} else if (a == b && b == c) {
return TriangleType.EQUILATERAL;
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
return TriangleType.RIGHT;
} else if (a != b && b != c && c != a) {
return TriangleType.SCALENE;
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
return TriangleType.ISOSCELES;
}
return TriangleType.NONE;
}
public static void main(String[] args) {
final TriangleType type = determineType(1, 1, 9);
System.out.println(type);
}
enum TriangleType {
EQUILATERAL, RIGHT, SCALENE, ISOSCELES, NONE
}
}
测试如下:
@Test
public void determineType() {
Assertions.assertAll(
() -> assertEquals("Should be isosceles", Triangle.TriangleType.ISOSCELES, Triangle.determineType(2, 2, 3)),
() -> assertEquals("Should be equilateral", Triangle.TriangleType.EQUILATERAL, Triangle.determineType(2, 2, 2)),
() -> assertEquals("Should be right", Triangle.TriangleType.RIGHT, Triangle.determineType(4, 3, 5))
);
}
试试这个代码,我已经修复了错误
测试代码
@Test
public void testMain(){
assertEquals(determineType(0,1,1),"Not a Triangle");
}
源代码
package triangle;
public class Triangle {
public static String determineType(int a, int b, int c) {
if (a >= (b + c) || c >= (b + a) || b >= (a + c)) {
return "Not a Triangle";
} else if (a == b && b == c) {
return "Equilateral Triangle";
} else if (((a * a) + (b * b)) == (c * c) || ((a * a) + (c * c)) == (b * b) || ((c * c) + (b * b)) == (a * a)) {
return "Right Triangle";
} else if (a != b && b != c && c != a) {
return "Scalene Triangle";
} else if ((a == b && b != c) || (a != b && c == a) || (c == b && c != a)) {
return "Isosceles Triangle";
}
// other case
return null;
}
public static void main(String[] args) {
System.out.println(determineType(1, 1, 9));
}
}