3

Disclaimer: I know this is a very bad design to actually have tests depend on each other to set any kind of variables. However I have to migrate these tests to Arquillian and rewriting everything is out of question.


Question: I have test methods that use some instance variables like this:

public int myNumber= 0;

@Test
public void testOne() {
// do something with myNumber
}

@Test(dependsOnMethods = "testOne")
public void testTwo {
// do something with myNumber

This used to work using the jboss microcontainer. But it doesn't, while running such test in-container using Arquillian. What is the easiest way to make this work again? Right now I simply made all the fields static, which works. Are there any negative consequences of that?


Edit: Cedric's suggestion doesn't work neither, which is probably due to the same reason instance variables don't work. Arquillian invokes the whole lifecycle for each @Test method and it looks like it injects a new ITestContext to each @Test as well. This is what I tried:

Integer number = new Integer(10);
static final String NUMBER = "number";

@Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void testOne(ITestContext ctx) {
    System.out.println("TEST ONE: " + number);
    number += 100;
    ctx.setAttribute(NUMBER, number);
    System.out.println("CONTEXT " + ctx.getName());
    System.out.println("CONTEXT " + ctx.getAttribute(NUMBER));
}

@Test(dependsOnMethods="testOne", dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void testTwo(ITestContext ctx) {
    System.out.println("TEST TWO: " + number);
    System.out.println("CONTEXT " + ctx.getName());
    System.out.println("CONTEXT " + ctx.getAttribute(NUMBER));
}

And result:

TEST ONE: 10
CONTEXT Arquillian - class com.example.ServerTest
CONTEXT 110
TEST TWO: 10
CONTEXT Arquillian - class com.example.ServerTest
CONTEXT null
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
  • It seems this is not possible, by design: [http://jayshaughnessy.blogspot.com/2012/11/arquillian-and-testng.html](http://jayshaughnessy.blogspot.com/2012/11/arquillian-and-testng.html) – Oliver Hernandez Mar 31 '15 at 14:09

2 Answers2

0

I'm not sure what caused it to stop running (it should work), but you could declare an ITestContext in the signature of both these methods, which will be injected by TestNG, and then call setAttribute/getAttribute to share parameters between your methods.

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55
  • It was not clear from my question: I am running the test using Arquillian in-container in JBoss 7. Arquillian invokes the whole lifecycle including the @After/@Before methods for each @Test method inside the class. – MartinTeeVarga Jun 14 '13 at 05:59
  • I've added results after I tried your suggestion into my question. Unless I did something wrong, it doesn't work (as I expected knowing Arquillian architecture). – MartinTeeVarga Jun 14 '13 at 06:27
0

Easiest way to share data between tests is to use a (CDI) managed bean.

@ApplicationScoped
public class MyTestContext
{
    @Produces @ApplicationScoped
    Map<String, Object> objectStore()
    {
        return new HashMap<>();
    }
}

public class IntegrationTests extends ArquillianTest
{
    @Inject
    Map<String, Object> objectStore; 

    @Test() @InSequence(1)
    public void testReadAllUsers()
    {
        objectStore.put("x", 100);
    }

    @Test() @InSequence(2)
    public void testCreateNewUser() 
    {
        assertThat((Integer)objectStore.get("x")).isEqualTo(100);
    }
}
arpadf
  • 413
  • 3
  • 13