I am using Apache-CXF-2.2.7 for rest services with spring 2.5 , Rest services working fine when I test them using postman , but not when I hit them using Webclient for unit testing ,it throws null pointer exception for spring beans which is autowired in rest service implementation. here is my code.
package com.mfg.qualityTest;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import junit.framework.Assert;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import com.mfg.quality.qualityService.RestQualityService;
import com.mfg.quality.qualityServiceImpl.RestQualityServiceImpl;
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
public class JAXRSTest extends AbstractApiTest {
private final static String ENDPOINT_ADDRESS = "http://localhost/quality-svc/api/v1/";
private final static String WADL_ADDRESS = ENDPOINT_ADDRESS + "?_wadl&_type=xml";
private static Server server;
@BeforeClass
public static void initialize() throws Exception {
startServer();
waitForWADL();
}
private static void startServer() throws Exception {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(RestQualityServiceImpl.class);
List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
RestQualityService rs = new RestQualityServiceImpl();
// add custom providers if any
sf.setProviders(providers);
sf.setServiceBeans(rs);
sf.setAddress(ENDPOINT_ADDRESS);
server = sf.create();
}
// Optional step - may be needed to ensure that by the time individual
// tests start running the endpoint has been fully initialized
private static void waitForWADL() throws Exception {
WebClient client = WebClient.create(WADL_ADDRESS);
// wait for 20 secs or so
for (int i = 0; i < 20; i++) {
Thread.currentThread();
Thread.sleep(1000);
Response response = client.get();
if (response.getStatus() == 200) {
break;
}
}
// no WADL is available yet - throw an exception or give tests a chance to run anyway
}
@AfterClass
public static void destroy() throws Exception {
server.stop();
}
@Test
public void testGetBookWithWebClient() {
WebClient client = WebClient.create(ENDPOINT_ADDRESS);
client.accept(MediaType.APPLICATION_JSON);
client.path("/quality/50");
client.type(MediaType.APPLICATION_JSON);
client.encoding( "UTF-8" );
client.header( "token","038c43b41ca247a29c5b87a1e8c64d21");
Response response = client.get();
if(response.getStatus()!=200){
throw new RuntimeException("HTTP Error: "+ response.getStatus());
}
Assert.assertTrue(response.getStatus() == 200);
}
}
Rest service methods is
@Override
public Response getQuality( Long qualId ) throws Exception
{
log.info( "============================================> Get Quality by Id" );
Map< String, Object > errors = new HashMap< String, Object >();
Contact contact = userDetailsService.getContact();
-------------------
-------------------------------------------
}
Exception throws at
Contact contact = userDetailsService.getContact();
userDetailsService is coming Null and throwing NullPointer exception while testing with postman userDetailsService is injecting properly and working fine . Please help me on this.
Aucun commentaire:
Enregistrer un commentaire