3

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());
}
asked Nov 8, 2018 at 7:27
1
  • 1
    include the stacktrace Commented Nov 8, 2018 at 7:35

1 Answer 1

2

At the beginning of your test class make sure the class is annotated with testrunner as bellow

@RunWith(MockitoJUnitRunner.class)
public class BillingCycleValidatorImplCreateBillingCycleActionTest {
}
James Dube
83810 silver badges13 bronze badges
answered Nov 8, 2018 at 7:31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.