We are moving from using TestNG with built-in JBoss to using Arquillian with a remote server.
We run a simple test that has a method annotated with @BeforeClass that does some test setup. After a multiple search, it looks like this installation method is called twice: once on the console, where we run the Maven command to run the test, and again when the test war is deployed to our remote server and the test run is executed. These are two separate JVMS - one outside the container, the other inside the container. My preference is to just run the latter.
Is this the behavior I should expect, or is there something that I might lose?
For now, we check whether we are in the container or not, and if so, we run our installation code. This works, but I would like to know if there is a better way.
Some fragments of our code (please ignore the simplicity of the code and the fact that the setupComponents method is really not needed here, there are much more complex tests that we transfer that will need this function):
public class BaseTest extends Arquillian { private static Log log = LogFactory.getLog( SeamTest.class ); @Deployment public static Archive<?> createDeployment() { // snip... basically, we create a test war here } /** * todo - there might be a better way to do this */ private boolean runningInContainer() { try { new InitialContext( ).lookup( "java:comp/env" ); return true; } catch (NamingException ex) { return false; } } @BeforeClass public void setupOnce() throws Exception { getLog().debug( "in setupOnce(): " + runningInContainer() ); if ( runningInContainer() ) { new ComponentTest() { protected void testComponents() throws Exception { setupComponents(); } }.run(); } } public User createUser() { // ... } public Log getLog() { // snip... } public UserDao getUserDao() { // ... } public abstract class ComponentTest { protected abstract void testComponents() throws Exception; public void run() throws Exception { try { testComponents(); } finally { } } } } public class UserDaoTest extends BaseTest { UserDao userDao; @Override protected void setupComponents() { getLog().debug( "in setupComponents: " + runningInContainer() ); userDao = getUserDao(); } @Test public void testGetUser() throws Exception { getLog().debug( "in testGetUser: " + runningInContainer() ); new ComponentTest() { protected void testComponents() throws Exception { User user0 = createUser(); user0.setName( "frank" ); userDao.merge( user0 ); User retrievedUser = userDao.findByName( "frank" ); assertNotNull( retrievedUser ); } }.run(); } }
This basically gives me a conclusion that looks like this:
On the console where mvn is running:
in setupOnce(): false
On jboss server:
in setupOnce(): true in setupComponents: true in testGetUser: true
Chris williams
source share