Java 为什么JUnit5没有';看不到我的异常测试?
我已经写了48种测试方法来测试我的课程作业。它们都不是例外测试,但工作正常 最后,我尝试测试我自己的异常,JUnit5没有看到它。这里有一些我的代码设计和异常测试的例子Java 为什么JUnit5没有';看不到我的异常测试?,java,testing,exception,junit,Java,Testing,Exception,Junit,我已经写了48种测试方法来测试我的课程作业。它们都不是例外测试,但工作正常 最后,我尝试测试我自己的异常,JUnit5没有看到它。这里有一些我的代码设计和异常测试的例子 import static org.junit.jupiter.api.Assertions.*; import org.junit.Test; import junit.framework.TestCase; public class Final_Tests extends TestCase { @Test
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import junit.framework.TestCase;
public class Final_Tests extends TestCase {
@Test
public static void testD4_setChair() {
Department testD = new Department("CSE 101","CSE 101!");
Teacher testT = new Teacher("Ali","Veli.com",testD,"CV");
testD.setChair(testT);
assertEquals(testT,testD.getChair());
}
测试非异常方法没有问题
问题在于:
@Test
public static void DepartmentMismatchException_Test() {
Department testD = new Department("CSE 101","CSE 101!");
Teacher testT = new Teacher("Mehmet", "mehmet.com", testD, "CV Mehmet");
Course testC1 = new Course(testD,101,"Test Course 1","Test Course 1 Desc.",testT,6);
String expMsg = "DepartmentMismatchException: Teacher in " + testT.getDepartment().getCode()+ ", Course in " +testC1.getDepartment().getCode();
DepartmentMismatchException dme = new DepartmentMismatchException(testT.getDepartment(), testC1.getDepartment());
assertEquals(expMsg, dme.toString());
}
代码设计是否有问题,比如作用域、静态或其他修饰符的使用,或者关于我的异常测试代码?我假设testT.getDepartment()将抛出DME如果是,请尝试使用
org.junit.Rule
@Rule
public ExpectedException myExpectedException = ExpectedException.none();
@Test
public void DepartmentMismatchException_Test() {
Department testD = new Department("CSE 101","CSE 101!");
Teacher testT = new Teacher("Mehmet", "mehmet.com", testD, "CV Mehmet");
Course testC1 = new Course(testD,101,"Test Course 1","Test Course 1
Desc.",testT,6);
String expMsg = "DepartmentMismatchException: Teacher in " +
testT.getDepartment().getCode()+ ", Course in "
+testC1.getDepartment().getCode();
testT.getDepartment(); // or any other st. which you expect it to throw DME
myExpectedException.expect(DepartmentMismatchException .class);
myExpectedException.expectMessage(expMsg);
}
摘自JUnit5
@Test
javadoc:
@测试方法不能是私有的或静态的,也不能返回值
因此,您应该从它们中删除static
关键字,测试应该正常工作
通常,异常测试以某种方式抛出异常,并验证它是否正确抛出。我看到您正在实例化它并验证消息是否符合预期,但最好的方法是:
@Test
public void DepartmentMismatchException_Test() {
Department testD = new Department("CSE 101","CSE 101!");
Teacher testT = new Teacher("Mehmet", "mehmet.com", testD, "CV Mehmet");
Course testC1 = new Course(testD,101,"Test Course 1","Test Course 1 Desc.",testT,6);
String expMsg = "DepartmentMismatchException: Teacher in " + testT.getDepartment().getCode()+ ", Course in " +testC1.getDepartment().getCode();
Exception thrownException = Assertions.assertThrows(DepartmentMismatchException.class, () -> /* Some code that throws your exception */);
assertEquals(expMsg, thrownException.toString() /* or maybe thrownException.getMessage() */);
}
您可以查看教程和更多示例。我认为异常测试意味着测试,在您的对象应该抛出异常的情况下,它确实会抛出预期的异常。Enes,请检查这是否有帮助。您可以测试引发异常的方法,如本文所述。实际上,在这些代码中没有任何东西会引发异常。我只想比较来自DepartmentMismatchException的toString()方法的消息,该方法是学生编写的,该消息应该是。(假设我是教师,学生的作业任务列表包括类、属性和方法)我相信您必须在该测试用例中引发异常,以便您可以断言它的ex.toString()从不将规则用于Junit5:assertThrows是检查异常的方法。此外:即使是JUnit4,从版本4.13开始,这种方式已经不再适用了。在任何情况下都应该抛出异常,对吗?我认为我可以创建一个异常对象并访问它们的方法,比如toString,并将其与assertEquals一起使用。因此,这是一种不好的做法。您不需要抛出它,但通常您会尝试这样做,以确保如果异常遵循它应该遵循的路径(被抛出),它的行为符合预期。尽管我认为在使用它的地方测试它会更好,但您可以做如下操作:
assertThrows(departmentmischException.class,()->抛出新的departmentmischException(/*您的参数*/)
然后断言消息是预期的。再说一次,老实说,我不会采用这种方法。