i have a unit test throwing NullPointer Exception when i put the test in another file, its running fine below is the piece of code. other tests in the same file are running without any errors.
@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());
}
-
1include the stacktraceMaciej Kowalski– Maciej Kowalski2018年11月08日 07:35:06 +00:00Commented Nov 8, 2018 at 7:35
1 Answer 1
At the beginning of your test class make sure the class is annotated with testrunner as bellow
@RunWith(MockitoJUnitRunner.class)
public class BillingCycleValidatorImplCreateBillingCycleActionTest {
}
answered Nov 8, 2018 at 7:31
TapiwaJoel Mudavanhu
532 silver badges8 bronze badges
Sign up to request clarification or add additional context in comments.