Steven Rudolf
Steven Rudolf

Reputation: 275

mock cdi interceptors during junit tests

I have a complex Java EE 6 app with a web module, an EJB module and some utility jars.

I want to do some integration tests with Junit. Therefore I use the openwebbeans cdi container (Thanks to Mr. Struberg http://struberg.wordpress.com/2012/03/17/controlling-cdi-containers-in-se-and-ee/) It works perfectly. I can start a complete cdi container in a Junit test.

My problem is that I have some interceptors in my application which cannot run in a Junit test (MQ-, persistence- and transaction-interceptors). So I want to mock these interceptor implementations.

Does anybody know how to do this?

Upvotes: 1

Views: 3249

Answers (4)

rmpestano
rmpestano

Reputation: 888

Another option of "vetoing" could be through Deltaspike @Exclude annotation. It can veto beans based on ProjectStage.

Example:

@Exclude(ifProjectStage = ProjectStage.UnitTest.class)
public class MyInterceptor {
}

Then in your test you can activate the project stage using Deltapike test control module, example:

@RunWith(CdiTestRunner.class)
@TestControl(projectStage = UnitTest.class)
public class TestStageControl {

@Test...

} 

Upvotes: 0

Steven Rudolf
Steven Rudolf

Reputation: 275

To whom it may concern ;-)

At the end I solved my issue with clean Java EE techniques. I provided a method which observes the ProcessAnnotatedType event. This method evaluates the type which is processed and if it is one of my interceptors, then I veto the processing.

public void processAnnotatedType(@Observes final ProcessAnnotatedType<?> event, final BeanManager manager) {
    if (event.getAnnotatedType().getJavaClass().equals(PrivilegeCheckingInterceptor.class)) {
        event.veto();
    }       
}

Upvotes: 1

vinga
vinga

Reputation: 2012

You can also run tests with embedded OpenEJB.

This link http://openejb.apache.org/examples-trunk/interceptors/ may be useful - perhaps setting property of 'openejb.deployments.classpath.exclude' could help.

Upvotes: 0

LightGuard
LightGuard

Reputation: 5378

Why not just test in the container of choice with Arquillian? The other option which comes to mind would be to add in interceptors with mock functionality and exclude the actual interceptor implementation when you start the CDI container.

Upvotes: 0

Related Questions