EJB3Unit testing no-tx-datasource
I am doing tests on an ejb3-project using ejb3unit http://ejb3unit.sourceforge.net/Session-Bean.html for testing. All my Services long for @PersistenceContext (UnitName=bla). I set up the ejb3unit.properties like this:
ejb3unit_jndi.1.isSessionBean=true
ejb3unit_jndi.1.jndiName=ejb/MyServiceBean
ejb3unit_jndi.1.className=com.company.project.MyServiceBean
everything works with the in-memory-database.
So now i want additionally test another servicebean with @PersistenceContext (UnitName=noTxDatasource) that goes for a defined in my datasources.xml:
<datasources>
<local-tx-datasource>
...
</local-tx-datasource>
<no-tx-datasource>
<jndi-name>noTxDatasource</jndi-name>
<connection-url>...</connection开发者_JAVA技巧-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>bla</user-name>
<password>bla</password>
</no-tx-datasource>
</datasources>
How do I tell ejb3unit to make this work:
Object object = InitialContext.doLookup("java:/noTxDatasource");
if (object instanceof DataSource) {
return ((DataSource) object).getConnection();
} else {
return null;
}
Currently it fails saying: javax.NamingException: Cannot find the name (noTxDataSource) in the JNDI tree Current bindings: (ejb/MyServiceBean=com.company.project.MyServiceBean)
How can I add this no-tx-datasource to the jndi bindings?
I hate answering my own questions, but I had some simple thought:
public void setUp() throws Exception {
OracleDataSource ds = new OracleDataSource();
ds.setServerName("localhost");
ds.setPortName(1521);
ds.setDatabaseName("database"); // SID
ds.setUser("user");
ds.setPassword("pass");
InitialContext ic = new InitialContext();
ic.add("noTxDatasource", ds);
}
This will additionally allow you to make the following lookup work:
Object object = InitialContext.doLookup("java:/noTxDatasource");
delivering a datasource (in this case oracle).
精彩评论