How to test web service with EJB3 annotation
samwun9988 Sep 10, 2011 8:14 AMHi,
I followed an EJB3 junit test example, it is based on jboss-ejb-api_3.1_spec.
The sample project is based on maven2 and use OpenEJB for junit test.
When I execute the command "mvn clean install", it thrown the following error:
INFO - Deployed Application(path=classpath.ear)
WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service
WARN - Injection data not found in JNDI context: jndiName='org.superbiz.testinjection.MoviesTest/service', target=org.superbiz.testinjection.MoviesTest/service
findAll
I don't understand why the service object is not populated to the above directory.
eg. the @EJB injection is failed when it is instantiating the CategoryManagerImple service object.
I am using the following setup in my junit test file:
@LocalClient
public class MoviesTest extends TestCase {
@EJB
private Movies movies;
@EJB
private CategoryManagerImpl service;
@Resource
private UserTransaction userTransaction;
@PersistenceContext
private EntityManager entityManager;
public void setUp() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
p.put("movieDatabase", "new://Resource?type=DataSource");
p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
InitialContext initialContext = new InitialContext(p);
// Here's the fun part
initialContext.bind("inject", this);
}
There may be some issue in my CategoryManagerImpl service class, but I couldn't figured out why this class can't be instantiated by @EJB in junit test:
Here is my CategoryManagerImpl.java file:
@Stateless
//@LocalBean
public class CategoryManagerImpl implements CategoryManager {
// @WebServiceRef(wsdlLocation = "META-INF/wsdl/www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services.wsdl")
@WebServiceRef(wsdlLocation = "http://www.ixsystems.com.au/category-ws-0.0.1-SNAPSHOT/services?wsdl")
private CategoryWebService_Service service;
private static final Logger logger = Logger.getLogger(CategoryManagerImpl.class);
@Override
public int add(int a, int b) {
return a+b;
}
}
I also attached my sample project here for reference.
Any suggestion would be very appreciated.
Thanks
Sam
-
OpenEJB-test-sample.zip 17.0 KB