Dear Team,
I have written below test class for my ClientIntegrationDataAPIServiceImpl class as below.
But I am getting nullpointer exception for below in my JUNIT Class
String actual = clientIntegrationDataAPIServiceImpl.requestCommunityCreation(clientCommunity);
****************** JUNIT CLASS ClientIntegrationDataAPIServiceImplTest********************
package ai.contentadmin.core.service;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.github.tomakehurst.wiremock.WireMockServer;
import ai.contentadmin.core.constants.Constants;
import ai.contentadmin.core.models.authorapi.ClientCommunity;
import io.wcm.testing.mock.aem.junit5.AemContext;
class ClientIntegrationDataAPIServiceImplTest {
private WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
@Rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
@InjectMocks
ClientIntegrationDataAPIServiceImpl clientIntegrationDataAPIServiceImpl;
@test
void requestCommunityCreation() throws IOException, URISyntaxException, LoginException, RepositoryException {
wireMockServer.stubFor(post("/search".concat(String.format(Constants.COMMUNITIES)))
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
ClientCommunity clientCommunity = new ClientCommunity();
clientCommunity.setName("TestCommunity");
clientCommunity.setUserId("userid");
clientCommunity.setType("client");
clientCommunity.setPrivate(true);
String expected = Constants.STATUS_OK;
String actual = clientIntegrationDataAPIServiceImpl.requestCommunityCreation(clientCommunity);
Assert.assertEquals("Response should match", expected, actual);
wireMockServer.stop();
}
}
------------------------ ClientIntegrationDataAPIService Java Class ----------------------
Solved! Go to Solution.
Views
Replies
Total Likes
The null pointer exception could be caused by several reasons. One possible reason is that the `clientIntegrationDataAPIService` mock object is not properly initialized. You can try adding the `@RunWith(MockitoJUnitRunner.class)` annotation to the test class to initialize the mock objects.
@RunWith(MockitoJUnitRunner.class)
class ClientIntegrationDataAPIServiceImplTest {
// rest of the code
}
If this does not solve the issue, you can provide more information about the exception message and stack trace to help identify the root cause of the problem.
Hi @subnaik
Based on the code you provided, it seems that the clientIntegrationDataAPIServiceImpl
object is not properly initialized in your JUnit test class, which is causing the NullPointerException
when you call the requestCommunityCreation
method.
To fix this issue, you need to properly initialize the clientIntegrationDataAPIServiceImpl
object in your JUnit test class. One way to do this is to use a mocking framework like Mockito to create a mock object of the ClientIntegrationDataAPIServiceImpl
class, and then inject this mock object into your test class using the @InjectMocks
annotation.
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import com.github.tomakehurst.wiremock.WireMockServer;
import ai.contentadmin.core.constants.Constants;
import ai.contentadmin.core.models.authorapi.ClientCommunity;
import io.wcm.testing.mock.aem.junit5.AemContext;
class ClientIntegrationDataAPIServiceImplTest {
private WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
@Rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
private ClientIntegrationDataAPIService clientIntegrationDataAPIService;
@InjectMocks
private ClientIntegrationDataAPIServiceImpl clientIntegrationDataAPIServiceImpl;
@Test
void requestCommunityCreation() throws IOException, URISyntaxException, LoginException, RepositoryException {
wireMockServer.stubFor(post("/search".concat(String.format(Constants.COMMUNITIES)))
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
ClientCommunity clientCommunity = new ClientCommunity();
clientCommunity.setName("TestCommunity");
clientCommunity.setUserId("userid");
clientCommunity.setType("client");
clientCommunity.setPrivate(true);
String expected = Constants.STATUS_OK;
Mockito.when(clientIntegrationDataAPIService.requestCommunityCreation(clientCommunity)).thenReturn(expected);
String actual = clientIntegrationDataAPIServiceImpl.requestCommunityCreation(clientCommunity);
Assert.assertEquals("Response should match", expected, actual);
wireMockServer.stop();
}
}
In this modified test class, we have added the @Mock
annotation to create a mock object of the ClientIntegrationDataAPIService
interface, and the @InjectMocks
annotation to inject this mock object into the clientIntegrationDataAPIServiceImpl
object. We have also used the Mockito.when
method to mock the requestCommunityCreation
method of the ClientIntegrationDataAPIService
interface, and return the expected result when this method is called.
I had run the script like below, but still I am getting null pointer exception
class ClientIntegrationDataAPIServiceImplTest {
private WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
@Mock
private ClientIntegrationDataAPIService clientIntegrationDataAPIService;
@InjectMocks
private ClientIntegrationDataAPIServiceImpl clientIntegrationDataAPIServiceImpl;
@Test
void requestCommunityCreation() throws IOException, URISyntaxException, LoginException, RepositoryException {
wireMockServer.stubFor(post("/search".concat(String.format(Constants.COMMUNITIES)))
.willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json")));
ClientCommunity clientCommunity = new ClientCommunity();
clientCommunity.setName("TestCommunity");
clientCommunity.setUserId("userid");
clientCommunity.setType("client");
clientCommunity.setPrivate(true);
String expected = Constants.STATUS_OK;
Mockito.when(clientIntegrationDataAPIService.requestCommunityCreation(clientCommunity)).thenReturn(expected);
String actual = clientIntegrationDataAPIServiceImpl.requestCommunityCreation(clientCommunity);
Assert.assertEquals("Response should match", expected, actual);
wireMockServer.stop();
}
The null pointer exception could be caused by several reasons. One possible reason is that the `clientIntegrationDataAPIService` mock object is not properly initialized. You can try adding the `@RunWith(MockitoJUnitRunner.class)` annotation to the test class to initialize the mock objects.
@RunWith(MockitoJUnitRunner.class)
class ClientIntegrationDataAPIServiceImplTest {
// rest of the code
}
If this does not solve the issue, you can provide more information about the exception message and stack trace to help identify the root cause of the problem.
@subnaik Did you find the suggestion helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies