this is the method for which junit is written
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;
}
This is the test case wriiten
@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);
}
errors org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
- -> at com.acentra.ssp.impl.SelfServiceCoreClaimImplTest.testIsSelfServicePortalServerAvailable_Success(SelfServiceCoreClaimImplTest.java:71)
- -> at com.acentra.ssp.impl.SelfServiceCoreClaimImplTest.testIsSelfServicePortalServerAvailable_Success(SelfServiceCoreClaimImplTest.java:72)
- -> at com.impl.SelfServiceCoreClaimImplTest.testIsSelfServicePortalServerAvailable_Success(SelfServiceCoreClaimImplTest.java:73)
Please remove unnecessary stubbings More info: javadoc for UnnecessaryStubbingException class.
at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:192)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Not calling mock … can someone help me on this
I would try:
public boolean isSelfServicePortalServerAvailable(String crmEndPoint) {
boolean isServerAvailable = false;
try {
HttpURLConnection connection = getHttpURLConnection(crmEndPoint);
connection.connect();
isServerAvailable = true;
connection.disconnect();
} catch (MalformedURLException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
return isServerAvailable;
}
private HttpURLConnection getHttpURLConnection(String crmEndPoint) throws IOException {
URL url = new URL(crmEndPoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
return connection;
}
And then test
@Mock
private HttpURLConnection mockConnection;
@Mock
private ISelfServiceDao selfServiceDao;
@Mock
private RestTemplate restTemplate;
@InjectMocks
@Spy
private SelfServiceCoreClaimImpl selfServiceCoreClaimImpl;
@Test
public void testIsSelfServicePortalServerAvailable_Success() throws IOException {
String crmEndPoint = "http://test-url.com";
doReturn(mockConnection).when(selfServiceCoreClaimImpl).getHttpURLConnection(crmEndPoint);
doNothing().when(mockConnection).connect();
doNothing().when(mockConnection).disconnect();
boolean result = selfServiceCoreClaimImpl.isSelfServicePortalServerAvailable(crmEndPoint);
assertTrue(result);
}
You can’t just inject mock for objects that are created inside the method (like the new URL(crmEndPoint);
).
The @InjectMocks
will automatically inject the mocks inside private
fields, or setters or constructor parameters.
If an object is created inside a method then you have to consider the following:
- Is this object a stateless? Then I have to receive it from the constructor or DI system and then mock it. So I have to refactor the method and remove the creation of the new object.
- Is this object a DTO? Then I don’t need to mock it.
- If none of the above, which means the object creation can’t be avoided, (probably your case as I can tell) then you have to use the
MockedConstruction
.
Also for the mocked construction to work you will need the following dependency in Maven:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>some version here</version>
<scope>test</scope>
</dependency>
I’ve done few tweaking in your junit, and it runs perfectly fine.
@Test
public void testIsSelfServicePortalServerAvailable_Success() throws IOException {
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);
}
7