レガシー システムの単体テスト (junit を使用) を作成する必要があります。テストする必要があるメソッドは静的メソッドを使用しており、それが呼び出されたかどうかを確認する必要があります。したがって、PowerMockito を使用する必要があります (「通常の」モックには、mockito を使用します)。
しかし、PowerMockito ステートメントをテスト内に含めると、Mockito は . で失敗しますorg.mockito.exceptions.misusing.UnfinishedStubbingException
。行にコメントするPowerMockito.mockStatic(Application.class), PowerMockito.doNothing().when(Application.class) and PowerMockito.verifyStatic()
と、UnfinishedStubbingExceptiondoes は発生しませんが、この方法では、IllegalArgumentException が発生したかどうかを確認できません。
テスト中のメソッドは次のようになります。
public class ClientMB {
public void loadClient(Client client) {
try {
if (client == null) {
throw new IllegalArgumentException("Client is mandatory!");
}
setClient(clientService.findById(client.getId()));
} catch (Exception ex) {
Application.handleException(ex);
}
}
}
テストは次のようになります。
@PrepareForTest({ Application.class })
@RunWith(PowerMockRunner.class)
public class ClientMBTest {
@Test
public final void testLoadClient() {
ClientService mockedClientService = Mockito.mock(ClientService.class);
Mockito.when(mockedClientService.findById(42L)).thenReturn(new Client());
PowerMockito.mockStatic(Application.class);
PowerMockito.doNothing().when(Application.class);
ClientMB cmb = new ClientMB(mockedClientService);
mb.loadClient(null);
PowerMockito.verifyStatic();
}
}
最新バージョンを使用して PowerMokito をインポートしました。
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
私が間違っていることは何ですか?どんなアドバイスでも大歓迎です。