Server: JBoss 7.1.1
EJB 3.0
Eclipse Juno
I am working through my first webservice project using ejb 3.0 and am running into a problem on my client. For my client, I made up a servlet. The problem is when I attempt:
CalculatorOps calculator = (CalculatorOps)context.lookup("java:global/EJBCalculatorWS/CalculatorImp!math.CalculatorOps");
I am getting ClassNotFoundException on the lookup. I got this jndi from my JBoss server.log
[org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named CalculatorImp in deployment unit deployment "EJBCalculatorWS.war" are as follows:
java:global/EJBCalculatorWS/CalculatorImp!math.CalculatorOps
java:app/EJBCalculatorWS/CalculatorImp!math.CalculatorOps
java:module/CalculatorImp!math.CalculatorOps
I have 2 web projects, WS and Client, both added to the server and WS is in the build path of Client.
-------------CODE------------
Interface
package math;
#Local
public interface CalculatorOps {
public int add(int a, int b);
public int subtract(int a, int b);
}
Class
package math;
#Stateless(mappedName="TheCalc")
#WebService
public class CalculatorImp implements CalculatorOps{
#Override
public int add(int a, int b) {
return a+b;
}
#Override
public int subtract(int a, int b) {
return a-b;
}
}
Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CalculatorOps calculator = (CalculatorOps)context.lookup("java:global/EJBCalculatorWS/CalculatorImp!math.CalculatorOps");
}
Fixed: I ended up fixing this problem by placing the client and the ejb packages together in one project, then using the new jndi. I would still like to know tho why across multiple projects, I cant add to build path and then use the jndi to reference the ejbs without a ClassNotFoundException thrown.
If you are using eclipse, you need to add the EJB project to the deployment assembly of your web project to get it working, Build path will only exist for compilation but you get the error during run time if I am right.
RightClick on your web project->Properties->Deployment Assembly->Add
Related
I am trying to write an integration test for JMS service which looks like something like this.
#JmsListener(destination = "mailbox", containerFactory = "myFactory")
public void receiveMessage(Email message) throws InterruptedException {
try {
sendEmail(message);
}catch (Exception e){
LOGGER.log(Level.SEVERE,"Failed to deliver email",e);
Thread.sleep(TimeUnit.SECONDS.toSeconds(Optional.of(retryInterval).orElse(5)));
throw e;
}
}
private void sendEmail(Email message){
...............
}
First of all, can I mock this some how? I tried mocking it, but when I send a message the spring boot application is calling the actual JMS bean not the mock one. Seems like this is not possible.
Even if this is not possible, can I at least aoutowire the bean and somehow check if the receiveMessage method is being invoked. Furthermore, if it is being invoked, the sendEmail part should be faked so that it does not do any work. I have a few ideas such as creating a subclass for testing, but not happy with either of them. So wanted to if you can suggest me a better work around?
One approach is to use different profiles for say development, integration test and production and annotate the different components and your integration test class accordingly.
#Component
#Profile("it")
public class MessageReceiverIT {
#JmsListener(destination = "mailbox", containerFactory = "myFactory")
public void receiveMessage(SimpleMessage email) {
log.info("Integration test pretend to receive {}", email);
// (...)
This is the Integration test that uses the same Application class as the real Application, but if a message is received the MessageReceiverIT.receiveMessage() method will be invoked instead of the production component:
#RunWith(SpringRunner.class)
#SpringBootTest(classes=Application.class)
#ActiveProfiles("it")
public class JmsIntegrationTest {
#Inject
ConfigurableApplicationContext context;
#Test
public void testSend() throws Exception{
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
jmsTemplate.convertAndSend("mailbox", new SimpleMessage("it", "we need more IT"));
// (...)
Also check out Spring Boot Testing for alternative approaches such as the use of #TestConfiguration. I'm using Spring Boot in my examples, but there should be similar approaches if you have a none Spring Boot Application.
Hi I'm trying to test my Service and Dao layers for a Java EE 7 application.
So I looking for testing solutions follow tutorials using Arquillian with junit test and wildfly remote dependence.
Dao and Service interfaces with relative implementations have been created, following my junit test with Arquillian:
#RunWith(Arquillian.class)
public class GenericServiceTest {
#Inject
private EmployeeService employeeService;
#Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap
.create(JavaArchive.class)
.addAsManifestResource("META-INF/persistence.xml",
"persistence.xml")
.addClasses(EmployeeDao.class, EmployeeDaoImpl.class,
EmployeeService.class, EmployeeServiceImpl.class,
Employee.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
#Test
#Transactional
public void should_crud() {
// Gets all the objects
assertNotNull(employeeService);
Employee employee = employeeService.get(new Integer(1));
assertNotNull(employee);
}
}
Running class as JUnit Test it doesn't work with this error:
Caused by: java.lang.Exception: "WFLYCTL0216: Management resource '[(\"deployment\" => \"test.war\")]' not found"
Test pass if any classes has been added to ShrinkWrap as following:
#RunWith(Arquillian.class)
public class GenericDaoTest {
#Inject
private EmployeeService employeeService;
#Deployment
public static JavaArchive createTestableDeployment() {
final JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return jar;
}
#Test
public void should_crud() {
}
}
How can I create a working test using arquillian for Java EE 7 adding service class implementations?
And I have To add every Class and Intefaces that have to be called (for example all entities,dao etc classes) or only Service Interface and implementation Class?
Thanks a lot
Since you're developing a javaee application, I would suggest you to create a War archive instead of Jar.
You can add the whole package using
ShrinkWrap.addPackages(true, "com.yourpackage.name") so you don't have to add your classes independently.
If I understand the question correctly you want to test a war archive.
If this is the case you should change
return ShrinkWrap
.create(JavaArchive.class)
to
return ShrinkWrap
.create(WarArchive.class)
In addition you should add your persistence.xml file to the META-INF folder like:
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
If you want to use the annotation #Transactional in your tests, you need to add a few dependencies to your test scope. If you didn't add them yet you can read at http://arquillian.org/modules/transaction-extension/ what dependencies to add.
I am trying to execute a RESTful web service deployed on tomcat using Jersey implementation.
Following is the resource class
#Path("/rest")
public class PatientResource {
PatientService patientService;
#GET
#Path("/patient/{patientId}")
#Produces(MediaType.APPLICATION_JSON)
public Patient getPatientDetails(#PathParam("patientId") String patientId) {
//return patientService.getPatientDetails(patientId);
return new PatientService().getPatientDetails(patientId);
}
#GET
#Path("/patients")
#Produces(MediaType.APPLICATION_JSON)
public PatientData<Patient> getAllPatients() {
//return patientService.getAllPatients();
return new PatientService().getAllPatients();
}
}
I have made necessary entries in web.xml and all necessary jars are available in classpath, however when application is started on tomcat and I enter the URL in browser, I get following exception
[Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError
at org.codehaus.jackson.map.AnnotationIntrospector$Pair.findSerializer(AnnotationIntrospector.java:1148)
at org.codehaus.jackson.map.ser.BasicSerializerFactory.findSerializerFromAnnotation(BasicSerializerFactory.java:367)
at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:252)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782)
Any idea how to resolve it ?
Probably an inconsistency in the versions of Jersey and Jackson you are using at runtime .
What are your libs versions ?
I'm developing an application to perform a series of tests on various web services. These web services consume and produce json, and for each of them we have a class to model the json request and response. For example:
If the json request for serviceX is something like this:
{
"name":"Alex",
"id":"123"
}
We have a class serviceXrequest like this:
public class serviceXrequest {
String name;
String id;
//Constructor, getters/setters, etc
...
}
With an object of that class as the starting point, we can perform a series of test on the web service. The idea is to make those test as generic as possible so they can be used with any web service by just writing a class that models it's request and a class to model the response.
For that reason, all of the test methods developed so far work with plain java objects. This is an example of what I want to have:
public class WebServiceTest {
String serviceURL;
String requestJson;
String requestClass;
String responseClass;
public WebServiceTest() {}
#Test
public static void Test1() { ... }
#Test
public static void Test2() { ... }
....
#Test
public static void TestN() { ... }
}
And then, from another class, invoke those tests with doing something like this:
public class LoginTest { //To test the login web service, for example
public static void main(String[] args) {
WebServiceTest loginTest = New WebServiceTest();
loginTest.setServiceURL("172.0.0.1/services/login");
loginTest.setRequestJson("{"user":"ale","pass":"1234"}");
...
loginTest.runTests();
}
}
I know it's not that simple, but any ideas on how to get there?
Thanks in advance!!
You might also look into REST-assured
One of the best tools for testing your webservices is SOAP UI, but this is more for functional testing
As well I integrated very well FitNesse tests
JMeter goes hand in hand with LoadUI ..kind of same things in terms of stress and load tests for webservices.
Junit...i never used directly applied to the webservice itself.
Most of the times I had a Spring service called by the implemetation of the WebService interface (Port) and I unit tested that one.
You should consider using http-matchers (https://github.com/valid4j/http-matchers) which let's you write JUnit-tests, using regular hamcrest-matchers (bundled with JUnit) to test your web-service via standard JAX-RS interface.
Hi I am developing a Spring -Jaxws web service using JBOSS 5. I am using "SimpleJaxWsServiceExporter" to deploy the service. My service endpoint is :
package com.pb.pts.spring.service;
#Component
#WebService(serviceName="ParcelTrackingService")
public class ParcelTrackingServiceEndpoint {
#Autowired
public ParcelTrackingService trackingService;
#WebMethod
public String createParcelDetails(ParcelDetails details) throws TrackingException{
return trackingService.createParcelDetails(details);
}
#WebMethod
public ParcelTrackingData getParcelTrackingDetails(ParcelTrackingRequestData requestData) throws TrackingException{
return trackingService.getParcelTrackingDetails(requestDa ta);
}
}
The TrackingException class is :
public class TrackingException extends Exception {
private TrackingError[] errors;
public TrackingException() {
super();
this.errors = null;
}
public TrackingError[] getErrors() {
return errors;
}
public void setErrors(TrackingError[] errors) {
this.errors = errors;
}
}
I get the following error while deploying on jboss :
org.jboss.ws.WSException: Property 'errors' not found in fault bean 'com.pb.pts.spring.service.jaxws.TrackingException Bean'
at org.jboss.ws.metadata.umdm.FaultMetaData.initializ eFaultBean(FaultMetaData.java:282)
at org.jboss.ws.metadata.umdm.FaultMetaData.eagerInit ialize(FaultMetaData.java:225)
at org.jboss.ws.metadata.umdm.OperationMetaData.eager Initialize(OperationMetaData.java:468)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerI nitializeOperations(EndpointMetaData.java:559)
at org.jboss.ws.metadata.umdm.EndpointMetaData.initia lizeInternal(EndpointMetaData.java:543)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerI nitialize(EndpointMetaData.java:533)
at org.jboss.ws.metadata.umdm.ServiceMetaData.eagerIn itialize(ServiceMetaData.java:433)
at org.jboss.ws.metadata.umdm.UnifiedMetaData.eagerIn itialize(UnifiedMetaData.java:194)
at org.jboss.wsf.stack.jbws.EagerInitializeDeployment Aspect.start(EagerInitializeDeploymentAspect.java: 48)
at org.jboss.wsf.framework.deployment.DeploymentAspec tManagerImpl.deploy(DeploymentAspectManagerImpl.ja va:129)
at org.jboss.wsf.container.jboss50.deployer.ArchiveDe ployerHook.deploy(ArchiveDeployerHook.java:76)
at org.jboss.wsf.container.jboss50.deployer.AbstractW ebServiceDeployer.internalDeploy(AbstractWebServic eDeployer.java:60)
at org.jboss.deployers.spi.deployer.helpers.AbstractR ealDeployer.deploy(AbstractRealDeployer.java:55)
at org.jboss.deployers.plugins.deployers.DeployerWrap per.deploy(DeployerWrapper.java:179)
... 29 more
Caused by: java.beans.IntrospectionException: Method not found: isErrors
at java.beans.PropertyDescriptor.<init>(PropertyDescr iptor.java:89)
at java.beans.PropertyDescriptor.<init>(PropertyDescr iptor.java:53)
at org.jboss.ws.metadata.umdm.FaultMetaData.initializ eFaultBean(FaultMetaData.java:271)
It say " Method not found: isErrors" inspite of the fact that errors is not a boolean.
Can you please provide some insight into this problem? Any help would be appreciated.
I found a solution to the problem.. This is the java doc for SimpleJaxwsServiceExporter.
"Note that this exporter will only work if the JAX-WS runtime actually
supports publishing with an address argument, i.e. if the JAX-WS runtime
ships an internal HTTP server. This is the case with the JAX-WS runtime
that's inclued in Sun's JDK 1.6 but not with the standalone JAX-WS 2.1 RI."
So a SimpleJaxwsServiceExporter web service implementation using spring will work on Tomcat but not on JBoss as JBoss has it own implementation of JAX-WS.
So I deployed a pure Jax-ws web service without spring support and that worked.