1

So I have code below-

@RunWith(MockitoJUnitRunner.class)
public class TeamSubscriptionServiceTest {

    @InjectMocks
    private TeamSubscriptionService teamSubscriptionService;

    @Mock
    private ImsCustomerProfileService imsService;

    @Mock
    private IUserService userService;

    @Mock
    private HttpRequestService httpRequestService;

    @Mock
    private ISubscriptionDbService subscriptionDbService;

    private String imsToken = "IMS_Token";

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        when(imsService.getAccessToken()).thenReturn(imsToken);
        ReflectionTestUtils.setField(teamSubscriptionService, "jilEndpoint", "www.testJil.com");
        ReflectionTestUtils.setField(teamSubscriptionService, "adobeIOApiKey", "api_key");
    }


    @Test(groups = { TestGroup.UNIT_TESTS })
    public void testAddSeat() throws IOException {

        String teamId = "TestTeamID";
        String locale = "En_US";
        String jasonValue = "TestJasonData";
        String apiCallContent = "addSeatAPIResult";

        HttpResponse addSeatResponse  = mock(HttpResponse.class);
        when(addSeatResponse.getCode()).thenReturn(200);
        when(addSeatResponse.getContent()).thenReturn(apiCallContent);

        HttpServletResponse response = mock(HttpServletResponse.class);
        when(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse);

        String result = teamSubscriptionService.addSeat(teamId,locale,jasonValue,response);
        assertNotNull(result);
        assertEquals(result, "addSeatAPIResult");
    }
}

When I test it I get a NullPointerException on the line

when(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse);

I feel that all the objects annotated with @Mock are somehow null and the object are not getting injected to the teamSubscriptionService object.

Any Idea whats wrong with the code?

1 Answer 1

1

The problem is that you are mixing TestNG and JUnit annotations.

Test method is annotated with @Test(groups = { TestGroup.UNIT_TESTS }) - it is clearly a TestNG annotation @org.testng.annotations.Test, because JUnit's equivalent does not have element called groups.

However, you are using JUnit's @Before annotation on setup() method, therefore this method is never invoked. TestNG equivalent for this annotation is @org.testng.annotations.BeforeTest. Use it instead.

<...>
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
<...>

public class TeamSubscriptionServiceTest {
    @InjectMocks
    private TeamSubscriptionService teamSubscriptionService;
    @Mock
    private ImsCustomerProfileService imsService;
    @Mock
    private IUserService userService;
    @Mock
    private HttpRequestService httpRequestService;
    @Mock
    private ISubscriptionDbService subscriptionDbService;

    private String imsToken = "IMS_Token";

    @BeforeTest
    public void setup() {
        MockitoAnnotations.initMocks(this);
        <...>
    }

    @Test(groups = { TestGroup.UNIT_TESTS })
    public void testAddSeat() throws IOException {
        <...>
    }
}

As a side note, @RunWith(MockitoJUnitRunner.class) is redundant as well, when using TestNG.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tapan I will appreciate a lot, if you accept my answer, since it works for you.
Done...I am new here...Had to figure out how to accept an answer ;)

Your Answer

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.