簡體   English   中英

為什么 jUnit 5 看不到我的異常測試?

[英]Why jUnit 5 doesn't see my exception test?

我編寫了 48 種測試方法來測試我的課程作業。 它們都不是一個特殊的測試,但它們工作正常。

最后,我嘗試測試自己的異常,JUnit 5 沒有看到。 有一些我的代碼設計和異常測試的例子。

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

代碼設計是否存在問題,例如使用范圍、static 或其他修飾符,或者關於我的異常測試代碼?

取自JUnit5 @Test javadoc:

@Test 方法不能是私有的或 static 並且不能返回值。

因此,您應該從中刪除static關鍵字,測試應該可以正常工作。

通常異常測試以某種方式throw異常並驗證它是否被正確thrown 我看到您正在實例化它並驗證消息是否符合預期,但最好的方法是:

@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() */);
}

您可以在此處查看教程和更多示例。

我假設 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM