【问题标题】:@Mock objects returning null@Mock 对象返回 null
【发布时间】:2016-11-08 10:02:49
【问题描述】:

所以我有下面的代码-

@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 注释的对象都不知何故为空,并且该对象没有被注入到 teamSubscriptionService 对象中。

知道代码有什么问题吗?

【问题讨论】:

    标签: junit nullpointerexception mocking mockito junit4


    【解决方案1】:

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

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

    但是,您在 setup() 方法上使用了 JUnit 的 @Before 注释,因此永远不会调用此方法。此注释的等效 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 {
            <...>
        }
    }
    

    附带说明,@RunWith(MockitoJUnitRunner.class) 在使用 TestNG 时也是多余的。

    【讨论】:

    • @Tapan 如果您接受我的回答,我将不胜感激,因为它对您有用。
    • 完成...我是新来的...必须弄清楚如何接受答案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多