例外の戻りコードをテストしたいと思います。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();
}
}
[テスト期待される例外のJUnitの正しい方法]の可能性のある重複したため
(expected = MyExceptionClass.class)
宣言は必要ありません。 http://stackoverflow.com/questions/42374416/junit-right-way-of-test-expected-exceptions) –私はそれを行う良い方法を探しています。試してみてください/キャッチはOKですが、より多くのコード行を意味します。それは私の視点で読むことは醜いです... – Guillaume
私の答えを確認してください、このアプローチがあなたを助けることを願って –