예외 리턴 코드를 테스트하고 싶습니다.JUnit 4로 사용자 정의 예외의 오류 코드를 테스트하십시오.
class A {
try {
something...
}
catch (Exception e)
{
throw new MyExceptionClass(INTERNAL_ERROR_CODE, e);
}
}
그리고 해당 예외 : 여기 내 생산 코드
class MyExceptionClass extends ... {
private errorCode;
public MyExceptionClass(int errorCode){
this.errorCode = errorCode;
}
public getErrorCode(){
return this.errorCode;
}
}
내 단위 테스트 : 간단한
public class AUnitTests{
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test (expected = MyExceptionClass.class,
public void whenRunningSomething_shouldThrowMyExceptionWithInternalErrorCode() throws Exception {
thrown.expect(MyExceptionClass.class);
??? expected return code INTERNAL_ERROR_CODE ???
something();
}
}
가능한 복제에 대한
(expected = MyExceptionClass.class)
선언이 필요하지 않습니다 http://stackoverflow.com/questions/42374416/junit-right-way-of-test-expected-exceptions) –나는 그것을 할 수있는 좋은 방법을 찾고있다. 시도/catch 괜찮아요하지만 더 많은 코드 라인을 의미합니다. 그건 내 관점에서 읽을 추한 ... – Guillaume
제 대답을 확인하십시오,이 접근법이 당신을 도울 것입니다. –