Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

JUnit

...

4

...

for

...

CDI

...

Java-SE

...

tests

...

If

...

just

...

plain

...

unit

...

testing

...

is

...

needed

...

without

...

JEE

...

APIs

...

it's

...

possible

...

to

...

use

...

the AbstractCdiAwareTest

Code Block
java
java
titleSimple JUnit 4 test with dependency injection
 {{AbstractCdiAwareTest}}

{code:java|title=Simple JUnit 4 test with dependency injection}
@RunWith(JUnit4.class)
public class SimpleJUnitTest extends org.apache.myfaces.extensions.cdi.test.junit4.AbstractCdiAwareTest
{
    @Inject
    private Logger logger;

    @Test
    public void testLogger()
    {
        assertNotNull(this.logger);
        this.logger.info("I'm a JUnit test-case and I can use dependency injection!");
    }
}
{code}

h1. JUnit 4 for CDI 

JUnit 4 for CDI Java-EE

...

tests

...

If

...

it's

...

required

...

to

...

test

...

e.g.

...

request

...

scoped

...

beans

...

it's

...

possible

...

to

...

use

...

the AbstractServletAwareTest

Code Block
java
java
titleSimple JUnit 4 test with dependency injection for the Servlet API
 {{AbstractServletAwareTest}}

{code:java|title=Simple JUnit 4 test with dependency injection for the Servlet API}
@RunWith(JUnit4.class)
public class SimpleJUnitTest extends org.apache.myfaces.extensions.cdi.test.junit4.AbstractServletAwareTest
{
    @Inject
    private RequestScopedBean requestScopedBean;

    @Test
    public void testLogger()
    {
        assertNotNull(this.requestScopedBean.getValue());
    }
}
{code}

If

...

it's

...

required

...

to

...

test

...

e.g.

...

grouped

...

conversation

...

scoped

...

beans

...

it's

...

possible

...

to

...

use

...

the AbstractServletAwareTest

Code Block
java
java
titleSimple JUnit 4 test with dependency injection for the JSF API
 {{AbstractServletAwareTest}}

{code:java|title=Simple JUnit 4 test with dependency injection for the JSF API}
@RunWith(JUnit4.class)
public class SimpleJUnitTest extends org.apache.myfaces.extensions.cdi.test.junit4.AbstractJsfAwareTest
{
    @Inject
    private ViewAccessScopedBean viewAccessScopedBean;

    @Test
    public void testLogger()
    {
        assertNotNull(this.viewAccessScopedBean.getValue());
    }
}
{code}

h1. Needed modules and dependencies
Because the 

Needed modules and dependencies

Because the unit-test

...

modules

...

will

...

never

...

be

...

needed

...

in

...

production

...

builds,

...

you

...

have

...

to

...

add

...

the

...

CODI-test

...

modules

...

manually,

...

even

...

if

...

you

...

are

...

using

...

one

...

of

...

the

...

all-in-one

...

packages.

...

You'll

...

need

...

at

...

least

...

the

...

myfaces-extcdi-junit-support-module

...

for

...

general

...

JUnit

...

support.

...

And

...

depending

...

on

...

your

...

needs,

...

you'll

...

also

...

need

...

the

...

myfaces-extcdi-owb-support-module

...

and

...

myfaces-extcdi-jsf-support-module

...

.

...

Don't

...

forget

...

to

...

also

...

include

...

the

...

needed

...

(test)modules

...

of

...

the

...

CDI

...

implementation.

...

In

...

case

...

of

...

OWB,

...

you'll

...

need

...

cditest

...

and

...

cditest-owb

...

from

...

the

...

org.apache.openwebbeans.test

...

group.

...

For

...

convenience,

...

here's

...

a

...

pom-snippet

...

to

...

pick

...

the

...

needed

...

dependencies

...

from:

{code:xml|title=Snippet from {{}}
Code Block
xml
xml
titleSnippet from
pom.xml
with
example
CODI-test-dependencies.
}
        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>${junit-version}</version>
        </dependency>

        <!-- CODI test modules -->
        <dependency>
            <groupId>org.apache.myfaces.extensions.cdi.test</groupId>
            <artifactId>myfaces-extcdi-junit-support-module</artifactId>
            <version>${myfaces.codi.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.extensions.cdi.test</groupId>
            <artifactId>myfaces-extcdi-owb-support-module</artifactId>
            <version>${myfaces.codi.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.extensions.cdi.test</groupId>
            <artifactId>myfaces-extcdi-jsf-support-module</artifactId>
            <version>${myfaces.codi.version}</version>
            <scope>test</scope>            
        </dependency>
        
        <!-- the CODI myfaces-extcdi-jsf-support-module depends on myfaces-test -->
        <dependency>
            <groupId>org.apache.myfaces.test</groupId>
            <artifactId>myfaces-test12</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>

        <!-- needed by myfaces test -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-el_1.0_spec</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency> 

        <!-- OWB test -->
        <dependency>
            <groupId>org.apache.openwebbeans.test</groupId>
            <artifactId>cditest</artifactId>
            <version>${owb.version}</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.openwebbeans.test</groupId>
            <artifactId>cditest-owb</artifactId>
            <version>${owb.version}</version>
            <scope>test</scope>
        </dependency>    
{code}

h1. Examples

An example is available [here|http://code.google.com/a/apache-extras.org/p/myfaces-codi-examples/source/browse/#hg%2Ftemplate]

Examples

An example is available here.