3

My JEE 6 Application is using CDI, as well as Arquillian with an embedded tomee (1.7.2) to test it. In a test-class with multiple test methods, the same Request Scoped Bean instance is being injected in multiple test methods. The problem is not occuring when the application is deployed on a stand-alone tomee plus 1.7.2.

According to the arquillian documentation a request scoped bean instance should only be used for one testmethod. Unfortunately tomee embedded itself it not mentioned in the Arquillian documentation. Is this a known limitation to tomee embedded? If not, how to fix it?

The Service Class:

@RequestScoped
public class SomeService
{
    private String user;

    public String execute(final String pNewUser)
    {
        if(user == null){
            user = pNewUser;
        } 
        return user;
    }
}

The Test-Class:

@RunWith(Arquillian.class)
public class TestCase
{

    @Inject
    private SomeService someService;

    @Deployment
    public static WebArchive createDeployment()
    {
        return DefaultMicroDeploymenCreator.createDefaultMicroDeplymentWithFileName("TestCase.war");
    }

    @Test
    public void testFirstTestMethod() throws Exception
    {
        String username = someService.execute("User A");
        Assert.assertEquals(username, "User A");
    }

    @Test
    public void testSecondTestMethod() throws Exception
    {
        String username = someService.execute("User B");
        Assert.assertEquals(username, "User B"); 
    }
}

The first test executed is success, the second one is failing.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

5

Do you have arquillian.xml in your tests? It should have

<defaultProtocol type="Servlet 3.0" />

to activate request scope iteraction. You probably also need to add the following dependency to your tests:

<dependency>
    <groupId>org.jboss.arquillian.protocol</groupId>
    <artifactId>arquillian-protocol-servlet</artifactId>
</dependency>

Please note that adding <protocol type="Servlet 3.0" /> to the <container> element only configures (does not select) an already selected protocol. If no protocol is selected using <defaultProtocol>, then the container's default is used.

dds
  • 2,335
  • 1
  • 31
  • 45
  • I was not able to set the default protocol due to an excpetion during the startup "Defined default protocol Servlet 3.0 can not be found on classpath." Looks like some libs are missing. Adding the protocol to the container tag was possible though - but it did not solve the problem. – Daniel Vermaasen Nov 25 '15 at 07:45
  • Okay, that made it. But what is the default protocol, which is causing this (at least for me) strange behaviour which is clearly against the documentation? – Daniel Vermaasen Nov 25 '15 at 08:15
  • 1
    It depends on container. Probably for your container it's JMX. See http://arquillian.org/guides/developing_a_container_adapter/ – dds Nov 25 '15 at 08:21
0

tomee embedded uses local protocol by default to benefit of the embedded case so you don't have independent request between test methods. Using servlet protocol solves it. Alternative is to use deltaspike cdictrl to start/stop the request scope in @Before/@After hooks.

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13