@Mock对象返回null

时间:2016-11-08 10:02:49

标签: junit nullpointerexception mocking mockito junit4

所以我的代码如下 -

@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");
    }
}

当我测试它时,我在行

上得到一个NullPointerException
when(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse);

我觉得用@Mock注释的所有对象都是null,并且对象没有被注入到teamSubscriptionService对象中。

任何想法代码都有问题吗?

1 个答案:

答案 0 :(得分:1)

问题在于您正在混合TestNG和JUnit注释。

测试方法用@Test(groups = { TestGroup.UNIT_TESTS })注释 - 它显然是TestNG注释@org.testng.annotations.Test,因为JUnit的等价物没有名为groups的元素。

但是,您在@Before方法上使用JUnit的setup()注释,因此永远不会调用此方法。此注释的TestNG等效项为@org.testng.annotations.BeforeTest。改为使用它。

<...>
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 {
        <...>
    }
}

作为旁注,使用TestNG时,@RunWith(MockitoJUnitRunner.class)也是多余的。