We're transitioning from using TestNG with an embedded JBoss to using Arquillian with a remote server.
We are running a simple test that has a method annotated with @BeforeClass that does some test setup. After a lot of digging, it looks like that setup method is being called twice: once on the console where we're executing our Maven command to run the test and again when the test war is deployed to our remote server and the test runs. These are two separate JVMS - one running outside the container and another running inside the container. My preference is to just have the latter run.
Is this the behavior I should expect or is there something I may be missing?
For now, we're actually checking to see if we're in the container or not and, if so, we run our setup code. This works but I'd like to know if there's a better way.
Some snippets of our code (please ignore the simplicity of the code and the fact that the setupComponents method really isn't needed here, there are much more complicated tests that we're migrating that will need this functionality):
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 output that looks like this:
From the console where mvn is being executed:
in setupOnce(): false
From the jboss server:
in setupOnce(): true
in setupComponents: true
in testGetUser: true
This is "expected" behaviour. Not really nice, but it is how Arqullian works.
JUnit
TestNG
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With