У меня есть модульный тест, генерирующий исключение NullPointer когда я помещаю тест в другой файл, он работает нормально ниже фрагмент кода. другие тесты в том же файле выполняются без ошибок.
@Test
public void validateShouldFailIfThereExistsABillingCycleWithSameId() {
final String expectedErrorMessage = "Billing Cycle with id " + id + " already exists";
when(billingCycleServiceAuditable.
findBillingCycleById(id, locale, DefaultAuth.SYSTEM_USER_NAME.getValue())).
thenReturn(Optional.of(billingCycle));
when(messageService.getMessage(I18Code.MESSAGE_CHARGING_BILLING_CYCLE_EXISTS.getCode(),
new String[]{id.toString()}, locale)).thenReturn(expectedErrorMessage);
when(mapper.map(billingCycle)).thenReturn(billingCycleDto);
when(mapper.map(billingCycleDto)).thenReturn(billingCycle);
final CommonResponse response = billingCycleValidator.validate(billingCycle, userAction, locale, DefaultAuth.SYSTEM_USER_NAME.getValue());
assertNotNull(response);
assertEq @Test
public void validateShouldFailIfThereExistsABillingCycleWithSameId() {
final String expectedErrorMessage = "Billing Cycle with id " + id + " already exists";
when(billingCycleServiceAuditable.
findBillingCycleById(id, locale, DefaultAuth.SYSTEM_USER_NAME.getValue())).
thenReturn(Optional.of(billingCycle));
when(messageService.getMessage(I18Code.MESSAGE_CHARGING_BILLING_CYCLE_EXISTS.getCode(),
new String[]{id.toString()}, locale)).thenReturn(expectedErrorMessage);
when(mapper.map(billingCycle)).thenReturn(billingCycleDto);
when(mapper.map(billingCycleDto)).thenReturn(billingCycle);
final CommonResponse response = billingCycleValidator.validate(billingCycle, userAction, locale, DefaultAuth.SYSTEM_USER_NAME.getValue());
assertNotNull(response);
assertEquals(expectedErrorMessage, response.getNarrative());
verify(billingCycleServiceAuditable,
times(1))
.findBillingCycleById(anyLong(), any(Locale.class), anyString());
}uals(expectedErrorMessage, response.getNarrative());
verify(billingCycleServiceAuditable,
times(1))
.findBillingCycleById(anyLong(), any(Locale.class), anyString());
}




В начале вашего тестового класса убедитесь, что класс аннотирован testrunner, как показано ниже.
@RunWith(MockitoJUnitRunner.class)
public class BillingCycleValidatorImplCreateBillingCycleActionTest {
}
включить трассировку стека