Monday, December 2, 2013

[add-on] cdi tests with deltaspike 0.5

arquillian is nice, however, several projects don't need/use it.
several of them implemented a custom approach to control the cdi-container, start/stop scopes and check invalid state (esp. after a failed test). usually the basic tasks are the same or very similar. therefore i prototyped a deltaspike test-ctrl add-on based on some use-cases:

//starts the container once and one session + request per test-method
@RunWith(CdiTestRunner.class)
public class ContainerAndInjectionControl
{
@Inject
private ApplicationScopedBean applicationScopedBean;
@Inject
private SessionScopedBean sessionScopedBean;
@Inject
private RequestScopedBean requestScopedBean;
//test the injected beans
}
//starts the container and session once and one request per test-method
@RunWith(CdiTestRunner.class)
@TestControl(startScopes = SessionScoped.class)
public class CustomizedScopeHandling
{
//inject beans and test them
}
//starts the container once (for the whole suite)
@RunWith(CdiTestSuiteRunner.class)
@Suite.SuiteClasses({
SimpleTest.class,
SimpleTestControlTest.class
})
public class SuiteLevelContainerControl
{
}
//fine-grained project-stage control
@RunWith(CdiTestRunner.class)
@TestControl(projectStage = CustomTestStage.class) //default: ProjectStage.UnitTest.class
public class TestStageControl
{
//tests here will see project-stage CustomTestStage.class
@Test
@TestControl(projectStage = ProjectStage.Development.class)
public void checkDevEnv()
{
}
//tests here will see project-stage CustomTestStage.class
}


Update:
This add-on is part of all versions of DeltaSpike after v0.5