-
Notifications
You must be signed in to change notification settings - Fork 0
TestBeans
TestBeans integrates JUnit 4.7 and later with Contexts and Dependency Injection (JSR-299) using the reference implementation JBoss Weld or Apache OpenWebBeans. This allows testing your CDI beans within Java SE environments.
It provides three new annotations which allow binding beans (e.g. resources) to test-related contexts: @SuiteScoped, @ClassScoped and @MethodScoped.
##License TestBeans is provided under Apache License 2.0.
##Getting started ###Before you continue There's no "official" 1.0 release yet, some of the APIs may still (slightly) change...
###Artifacts There are currently no binary artifacts available, they will be provided soon. Until then you will have to checkout the code and build it using Apache Maven 2.x/3.x (mvn install). This will deploy TestBeans to your local repository. Now you can use TestBeans in your test projects by adding dependencies to your pom.xml:
<!-- JUnit support -->
<dependency>
<groupId>com.buschmais.testbeans</groupId>
<artifactId>testbeans.junit</artifactId>
<version>0.9.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- If you want to use Weld... -->
<dependency>
<groupId>com.buschmais.testbeans</groupId>
<artifactId>testbeans.container.weld</artifactId>
<version>0.9.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- ...or if you prefer OpenWebBeans -->
<dependency>
<groupId>com.buschmais.testbeans</groupId>
<artifactId>testbeans.container.owbse</artifactId>
<version>0.9.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
Hence that TestBeans itself defines dependencies to the following artifacts:
- JUnit 4.9b3 (currently only available as beta release), JUnit 4.7+ can be used
- Weld SE 1.1.1 or Apache OpenWebBeans 1.1.0 (standalone container providing CDI services)
- SLF4J API 1.5.10 (logging framework).
###CDI container and suite context The @RunWith annotation must be used in combination with @CdiContainer to activate a CDI container:
@RunWith(TestBeansSuite.class)
@SuiteClasses({MyTest1.class, MyTest2.class})
// Declare the CDI container, Weld...
@CdiContainer(WeldSE.class)
// ...or OpenWebBeans
@CdiContainer(OpenWebBeansSE.class)
public class MySuiteTest {
}
The annotation @SuiteScoped can now be used to make beans available in a suite context. It is activated before the suite is started and deactivated after the suite has finished.
###Class context and method context Now add one of the following rules to your test class:
public class MyTest {
// JUnit 4.9: @ClassScoped and @MethodScoped are supported
@Rule @ClassRule public static TestRule weldRule = new TestBeansRule()
// JUnit 4.7/4.8: only @MethodScoped is supported
@Rule public static MethodRule weldRule = new TestBeansMethodRule();
...
}
This will provide class and method contexts. Their lifecycle is defined as follows:
- @ClassScoped
- activated before any method (including @BeforeClass methods) of a test class is run.
- deactivated after all methods (including @AfterClass methods) of a test class were run.
- @MethodScoped
- activated before a test method (including @Before methods) is run.
- deactivated after a test method (including @After methods) were run.
###Context related events Events are fired every time a context (suite, class or method) is activated or deactivated. They may be observed as follows:
public void beforeSuiteContext(@Observes @Before SuiteDescription description) { ... }
public void afterSuiteContext(@Observes @After SuiteDescription description) { ... }
public void beforeClassContext(@Observes @Before ClassDescription description) { ... }
public void afterClassContext(@Observes @After ClassDescription description) { ... }
public void beforeMethodContext(@Observes @Before MethodDescription description) { ... }
public void afterMethodContext(@Observes @After MethodDescription description) { ... }
###Accessing beans and firing events The TestContextManager is the adapter between the CDI container and the test framework. It allows you to obtain beans within your tests:
TestContextManager testContextManager = TestContextManager.getInstance();
testContextManager.get(beanClass);
testContextManager.get(beanClass, new AnnotationLiteral<MyQualifier>() {});
You may also fire events:
testContextManager.getBeanManager().fire(eventInstance, new AnnotationLiteral<MyQualifier>() {});
For further information on CDI and the usage of APIs refer to the Weld documentation.
##Feature requests and bug reporting If you are missing features or bugs appear you may raise issues here .
Suggestions or feedback are welcome and may be provided to dirk.mahler (at) buschmais.com.
Enjoy!