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