Reputation: 292
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());
}
Upvotes: 3
Views: 81
Reputation: 53
At the beginning of your test class make sure the class is annotated with testrunner
as bellow
@RunWith(MockitoJUnitRunner.class)
public class BillingCycleValidatorImplCreateBillingCycleActionTest {
}
Upvotes: 2