You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

JUnit 4 for CDI Java-SE tests

If just plain unit testing is needed without JEE APIs it's possible to use the AbstractTest

Simple JUnit 4 test with dependency injection
@RunWith(JUnit4.class)
public class SimpleJUnitTest extends org.apache.myfaces.extensions.cdi.test.junit4.AbstractTest
{
    @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!");
    }
}

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

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());
    }
}

If it's required to test e.g. grouped conversation scoped beans it's possible to use the AbstractServletAwareTest

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());
    }
}

Examples

An example is available here.

  • No labels