Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My advice to you would be to not fall into the common trap I see, which is to think you need to chose between mocking and using an embedded EJB container.</p> <p>You can use both, you should use both, and where you find it difficult to use both you should demand better support and more features from your EJB container.</p> <p>Certainly, you will find people at OpenEJB really supportive and more than happy to add features to support getting the best of both worlds. Nearly all the really good features have been created around the requests of users trying to do very specific things and finding it hard.</p> <h1>Standard EJBContainer API</h1> <pre><code>package org.superbiz.stateless.basic; import junit.framework.TestCase; import javax.ejb.embeddable.EJBContainer; public class CalculatorTest extends TestCase { private CalculatorBean calculator; /** * Bootstrap the Embedded EJB Container * * @throws Exception */ protected void setUp() throws Exception { EJBContainer ejbContainer = EJBContainer.createEJBContainer(); Object object = ejbContainer.getContext().lookup("java:global/simple-stateless/CalculatorBean"); assertTrue(object instanceof CalculatorBean); calculator = (CalculatorBean) object; } </code></pre> <p>Full source <a href="http://ci.apache.org/projects/openejb/examples-generated/simple-stateless/src/test/java/org/superbiz/stateless/basic/CalculatorTest.java.html" rel="noreferrer">here</a></p> <p>This scans the classpath and loads all beans.</p> <h1>No-scanning, easier mocking approach</h1> <p>Slightly different approach where you define everything in code. Obviously mocking is easier as you can supply mock implementations of beans where needed at will.</p> <pre><code>@RunWith(ApplicationComposer.class) public class MoviesTest extends TestCase { @EJB private Movies movies; @Resource private UserTransaction userTransaction; @PersistenceContext private EntityManager entityManager; @Module public PersistenceUnit persistence() { PersistenceUnit unit = new PersistenceUnit("movie-unit"); unit.setJtaDataSource("movieDatabase"); unit.setNonJtaDataSource("movieDatabaseUnmanaged"); unit.getClazz().add(Movie.class.getName()); unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)"); return unit; } @Module public EjbJar beans() { EjbJar ejbJar = new EjbJar("movie-beans"); ejbJar.addEnterpriseBean(new StatefulBean(MoviesImpl.class)); return ejbJar; } @Configuration public Properties config() throws Exception { Properties p = new Properties(); p.put("movieDatabase", "new://Resource?type=DataSource"); p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver"); p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb"); return p; } @Test public void test() throws Exception { userTransaction.begin(); try { entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); List&lt;Movie&gt; list = movies.getMovies(); assertEquals("List.size()", 3, list.size()); for (Movie movie : list) { movies.deleteMovie(movie); } assertEquals("Movies.getMovies()", 0, movies.getMovies().size()); } finally { userTransaction.commit(); } } } </code></pre> <p><a href="http://ci.apache.org/projects/openejb/examples-generated/application-composer/src/test/java/org/superbiz/composed/MoviesTest.java.html" rel="noreferrer">Full source here</a></p> <h1>The end result</h1> <p>It's tempting to focus on the differences between different types of testing, etc. but certainly there's something to be said for a pragmatic middle. I personally don't see anything wrong with being able to mix "unit" and "integration" styles as fluently as possible.</p> <p>Certainly, it's an admirable goal. Ideas and feature requests to get us closer are very welcome.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload