这是编写 junit 的方法
public boolean isSelfServicePortalServerAvailable(String crmEndPoint) {
boolean isServerAvailable = false;
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(crmEndPoint);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
isServerAvailable = true;
connection.disconnect();
} catch (MalformedURLException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
return isServerAvailable;
}
这是编写的测试用例
@Mock
private URL mockUrl;
@Mock
private HttpURLConnection mockConnection;
@Mock
private ISelfServiceDao selfServiceDao;
@Mock
private RestTemplate restTemplate;
@InjectMocks
private SelfServiceCoreClaimImpl selfServiceCoreClaimImpl = new SelfServiceCoreClaimImpl(restTemplate);
@Test
public void testIsSelfServicePortalServerAvailable_Success() throws IOException {
when(mockUrl.openConnection()).thenReturn(mockConnection);
doNothing().when(mockConnection).connect();
doNothing().when(mockConnection).disconnect();
boolean result = selfServiceCoreClaimImpl.isSelfServicePortalServerAvailable("http://test-url.com");
assertTrue(result);
}
错误org.mockito.exceptions.misusing.UnnecessaryStubbingException: 检测到不必要的存根。 干净且可维护的测试代码需要零不必要的代码。 以下存根是不必要的(单击以导航到相关代码行):
不调用模拟...有人可以帮助我吗
我在你的junit中做了一些调整,它运行得很好。
@Test
@SneakyThrows
public void testIsSelfServicePortalServerAvailable_Success() {
HttpURLConnection mockConnection = Mockito.mock(HttpURLConnection.class);
URL mockUrl = Mockito.mock(URL.class);
SelfServiceCoreClaimImpl selfServiceCoreClaimImpl= new SelfServiceCoreClaimImpl();
Mockito.doReturn(mockConnection).when(mockUrl).openConnection();
Mockito.doNothing().when(mockConnection).connect();
Mockito.doNothing().when(mockConnection).disconnect();
boolean result = selfServiceCoreClaimImpl.isSelfServicePortalServerAvailable("http://test-url.com");
assertTrue(result);
}
您不能只为方法内创建的对象注入模拟(如
new URL(crmEndPoint);
)。
@InjectMocks
会自动将模拟注入到 private
字段、setter 或构造函数参数中。
如果在方法内部创建对象,则必须考虑以下事项:
MockedConstruction
。
此外,为了使模拟构造正常工作,您将需要 Maven 中的以下依赖项:<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>some version here</version>
<scope>test</scope>
</dependency>