質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

986閲覧

JUnit privateメソッドの例外処理をテスト

yamame01

総合スコア16

JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2023/02/22 09:46

編集2023/02/22 11:37

実現したいこと

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>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2023/02/22 10:51 編集

各ツールのバージョン等は書かれたほうが良いと思います。 また、サンプル化したコードも実際にコンパイルして動く(同じ現象が再現できている)かどうかは確認されたほうが良いと思います。(現状ではコンパイルエラーになります。) MyCustomException はどのような定義でしょうか。
yamame01

2023/02/22 11:38

失礼いたしました。 ソースの修正&情報追加しました!
jimbe

2023/02/22 11:46

ありがとうございます。
guest

回答1

0

ベストアンサー

投稿2023/02/22 11:18

jimbe

総合スコア12646

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yamame01

2023/02/22 11:46

ありがとうございます!! まさにいただいたリンクの方法で、確認することができました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問