Combining Arquillian, JUnit, Servlet 3.0 and Weld

I'm trying to get all these disparate things working together for some unit testing.

So the basic program structure is simple Servlet 3.0 running on TomCat as a WebApp maven archetype. Using Weld as an implementation of CDI to inject service objects into the Servlets.

Which is all running.

My problem currently is in the unit tests. I don't want to be running Integration Tests so I want to use the dependency injection to add some mock Service objects to the Service and fake some API calls.

So I've tried some approaches like this: https://community.jboss.org/wiki/CreatingUnitTestsWithWeldAndJunit4 For making a custom runner for JUnit to run the CDI, however this always failed to actually inject anything into the Servlet object I instantiated, it could inject into the Test class itself though.

So I'm trying Arquillian having gone over the documentation: http://arquillian.org/guides/getting_started/?utm_source=cta

However I can't get this to run as it either can't find the container or I get Error could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunner

Maven:

   <dependencies>
    <!-- Testing dependencies -->
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.1.2.Final</version>
        <scope>import</scope>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
            <version>2.0.2</version>
            <!--  2.0.0-beta-4 is not working  ** we are using old version -->
            <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.jboss.shrinkwrap.descriptors</groupId>
            <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
            <version>2.0.0-alpha-5</version>
            <scope>test</scope>
    </dependency>

    <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven</artifactId>
            <version>2.1.0-alpha-1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.arquillian.container</groupId>
        <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
        <scope>test</scope>
        <version>1.1.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld</groupId>
        <artifactId>weld-core</artifactId>
        <version>2.1.2.Final</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.1.2.Final</version>
    </dependency>

    <!-- Servlet 3.0 APIs -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>7.0.30</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Test code:

   @RunWith(Arquillian.class)
public class TestSessionServlet {

    @Deployment
    @OverProtocol("Servlet 3.0")
    @TargetsContainer("weld")
    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(JedisSessionDao.class)
                .addAsLibraries(resolver.artifact("org.jboss.weld.servlet:weld-servlet"))
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
                        .addAsManifestResource("in-container-context.xml",
                        "context.xml").setWebXML("in-container-web.xml");



    }

    @Test
    public void testServlet() throws Exception {
        Assert.fail("Not yet implemented");

    } 

Is this the right approach or do I really need to use Tomcat embedded containers? Which seems like integration testing. My plan was to use Mockito to create faked HttpRequest and Response objects and redirect the Response Writer to a StringWriter. I got all that part running it was just the CDI that I couldn't manage.

Thanks in advance

Answers


OK what solved this for me was moving from:

<dependency>
    <groupId>org.jboss.weld.arquillian.container</groupId>
    <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
    <scope>test</scope>
    <version>1.1.2.Final</version>
</dependency>

To

    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-weld-se-embedded-1.1</artifactId>
        <scope>test</scope>
        <version>1.0.0.CR7</version>
    </dependency>

Need Your Help

How to generate date in SQL Query?

sql date insert dateadd

I want to generate date value since 1/1/2011 - 31/12/2011 on sql query.

Workaround for python 2.4's yield not allowed in try block with finally clause

python yield python-2.4 try-finally

I'm stuck on python2.4, so I can't use a finally clause with generators or yield. Is there any way to work around this?