実現したいこと
privateメソッドの中でExceptionを投げています。
事情により、(呼出元であるpublicメソッドではなく)privateメソッドのテストの必要があり
例外の内容を確認したいですが、try {} catch {}でうまくcatchできません。
該当のソースコード
テスト対象クラス
Java
1public class TargetClass { 2 public void publicMethod(Integer i) { 3 privateMethod(i); 4 } 5 6 private void privateMethod(Integer i) { 7 if (i == 0) { 8 throw new MyCustomExcetion(this.getClass(), "error message01"); 9 } else { 10 throw new MyCustomExcetion(this.getClass(), "error message02"); 11 } 12 } 13}
テストクラス
Java
1import org.junit.Test; 2import org.junit.runner.RunWith; 3import org.mockito.InjectMocks; 4import org.powermock.api.mockito.PowerMockito; 5import org.powermock.modules.junit4.PowerMockRunner; 6 7import java.lang.reflect.Method; 8 9import static org.hamcrest.MatcherAssert.assertThat; 10import static org.hamcrest.Matchers.is; 11 12@RunWith(PowerMockRunner.class) 13public class TargetClassTest { 14 @InjectMocks 15 TargetClass target; 16 17 @Test 18 public void privateTest() throws Exception { 19 Method method = TargetClass.class.getDeclaredMethod( 20 "privateMethod", Integer.class); 21 method.setAccessible(true); 22 23 TargetClass spy = PowerMockito.spy(target); 24 25 try { 26 method.invoke(spy, 1); 27 } catch (MyCustomException e) { 28 assertThat(e.getErrorMessage(), is("error message02")); // catchできる想定だが、デバックしてもここに来ない 29 } 30 } 31}
独自Exception
Java
1@Slf4j 2public class MyCustomException extends RuntimeException { 3 private static final Logger logger = LoggerFactory 4 .getLogger(OrangeItemLogic.class); 5 6 public String errorMessage; 7 8 public MyCustomException(Class clazz, String errorMessage) { 9 this.errorMessage = errorMessage; 10 logger.error(errorMessage); 11 } 12 13 public String getErrorMessage() { 14 return this.errorMessage; 15 }
試したこと
①発生しているExceptionを確認
Java
1 @Test 2 public void privateTest() throws Exception { 3 Method method = TargetClass.class.getDeclaredMethod( 4 "privateMethod", Integer.class); 5 method.setAccessible(true); 6 7 TargetClass spy = PowerMockito.spy(target); 8 9 try { 10 method.invoke(spy, 1) 11 } catch (MyCustomException e) { 12 assertThat(e.getErrorMessage(), is("error message02"); // catchできる想定だが、デバックしてもここに来ない 13 } catch (Exception e) 14 assertThat(e.getClass(), is(MyCustomException.class); // java.lang.AssertionErrorが発生している 15 } 16 }
②expectedを確認
→成功するが、ErrorMessageを確認したいため不十分。
Java
1 @Test(expected = MyCustomException.class) 2 public void privateTest() throws Exception { 3 Method method = TargetClass.class.getDeclaredMethod( 4 "privateMethod", Integer.class); 5 method.setAccessible(true); 6 7 TargetClass spy = PowerMockito.spy(target); 8 9 try { 10 method.invoke(spy, 1) 11 } catch (MyCustomException e) { 12 assertThat(e.getErrorMessage(), is("error message02"); // catchできる想定だが、デバックしてもここに来ない 13 } 14 }
ツール
pom.xml(抜粋)
xml
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-test</artifactId> 4 <scope>test</scope> 5 <exclusions> 6 <exclusion> 7 <groupId>org.slf4j</groupId> 8 <artifactId>slf4j-api</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 13 <dependency> 14 <groupId>org.assertj</groupId> 15 <artifactId>assertj-core</artifactId> 16 </dependency> 17 18 <dependency> 19 <groupId>io.takari.junit</groupId> 20 <artifactId>takari-cpsuite</artifactId> 21 <version>1.2.7</version> 22 <scope>test</scope> 23 </dependency> 24 <dependency> 25 <groupId>org.mockito</groupId> 26 <artifactId>mockito-core</artifactId> 27 <scope>test</scope> 28 </dependency> 29 30 <dependency> 31 <groupId>org.powermock</groupId> 32 <artifactId>powermock-api-mockito2</artifactId> 33 <version>2.0.0-beta.5</version> 34 <scope>test</scope> 35 </dependency> 36 <dependency> 37 <groupId>org.powermock</groupId> 38 <artifactId>powermock-module-junit4</artifactId> 39 <version>2.0.0-beta.5</version> 40 <scope>test</scope> 41 </dependency> 42 <dependency> 43 <groupId>uk.co.datumedge</groupId> 44 <artifactId>hamcrest-json</artifactId> 45 <version>0.2</version> 46 <scope>test</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.powermock</groupId> 50 <artifactId>powermock-core</artifactId> 51 <version>2.0.0-beta.5</version> 52 <scope>test</scope> 53 </dependency> 54 <dependency> 55 <groupId>org.easetech</groupId> 56 <artifactId>easytest-core</artifactId> 57 <version>1.4.0</version> 58 <scope>test</scope> 59 </dependency> 60 <dependency> 61 <groupId>org.dbunit</groupId> 62 <artifactId>dbunit</artifactId> 63 <version>2.5.4</version> 64 <scope>test</scope> 65 </dependency> 66 <dependency> 67 <groupId>org.apache.poi</groupId> 68 <artifactId>poi</artifactId> 69 <version>3.17</version> 70 <scope>test</scope> 71 </dependency> 72 73 <dependency> 74 <groupId>org.springframework.security</groupId> 75 <artifactId>spring-security-test</artifactId> 76 <scope>test</scope> 77 </dependency>
回答1件
あなたの回答
tips
プレビュー