2015-06-08 35 views
1

當我使用@RuleExpectedException(如下面第一行(adapted post)所示)時,在(*)的Class2testTest類中出現錯誤「未處理的異常類型」。如何使用JUnit Test註釋來聲明IOException消息?

package class2test; 

import java.io.IOException; 

public class Class2test { 
    public Class2test() throws IOException{ 
     throw new IOException("j"); 
    } 
} 

package test; 

import java.io.IOException; 

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.rules.ExpectedException; 

import class2test.Class2test; 

public class Class2testTest { 

    @Rule 
    public ExpectedException expectedEx = ExpectedException.none(); 

    @Test 
    public void test() { 
     expectedEx.expect(IOException.class); 
     expectedEx.expectMessage("j"); 
     new Class2test(); //(*) 
    } 

} 

我不明白這一點:adapted post是可重複的(有一個RuntimeException,而一個IOException,它的工作原理)。

回答

1

應指定在方法的簽名拋出的異常:

public void test() throws Exception {