【发布时间】: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