1
所以,我有如下─代码@Mock对象返回null
@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对象中。
任何想法什么是错的代码?
非常感谢Myk ....它的工作:) – Tapan
@Tapan我会很感激,如果你接受我的答案,因为它适合你。 –
完成...我是新来的...必须弄清楚如何接受答案;) – Tapan