@Mock 对象返回 null
@Mock objects returning 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 对象中。
知道代码有什么问题吗?
问题是您混合了 TestNG 和 JUnit 注释。
测试方法用 @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 时也是多余的。
所以我有下面的代码-
@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");
}
}
当我测试它时,我在
行收到 NullPointerExceptionwhen(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse);
我觉得所有用@Mock 注释的对象都是空的,并且对象没有被注入到 teamSubscriptionService 对象中。
知道代码有什么问题吗?
问题是您混合了 TestNG 和 JUnit 注释。
测试方法用 @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 时也是多余的。