Hi Xavier,
first test doesnt pass cause app composer runs the test as an EJB
(ManagedBean actually) and therefore inherits from the auto request scope
for it. RequestScoped is quite particular cause often implicit so switching
it off is quite an issue for a lot of tests - it explains why it is still
on even if ScopesRule is active. We can think to a way to force it to be
disabled but I think it would need to be another flag to not break too much
tests (like new ScopesRule(disableRequestScope) or something like that).
regarding "is this a bug" the answer is no.
ok ok, I'll explain why ;). This is a conflict between junit runner and
rules, you need to migrate to app composer rule (which is as hard as
removing the runner and using ApplicationComposerRule) and chain it in an
ordered manner with the scope rule:
@ContainerProperties({
@ContainerProperties.Property(name =
"openejb.testing.start-cdi-contexts", value = "false")
})
@org.apache.openejb.testing.Classes(cdi = true, innerClassesAsBean = true)
public class ScopesRuleTest {
@Rule
public final TestRule rule = RuleChain.outerRule(new
ApplicationComposerRule(this)).around(new ScopesRule());
public static class Foo {
public void touch() {
// ok
}
}
@Inject
private BeanManager beanManager;
@Test
public void scopeDoesNotExist() {
assertFalse(beanManager.getContext(RequestScoped.class).isActive());
}
@Test
@CdiScopes(RequestScoped.class)
public void scopeExists() {
assertTrue(WebBeansContext.currentInstance().getContextsService().getCurrentContext(RequestScoped.class).isActive());
}
}
Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> | Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>
2016-12-16 18:37 GMT+01:00 Xavier Dury <[email protected]>:
> Hi,
>
> I have a problem with the following test (with TomEE 7.0.1):
>
> @ContainerProperties(@ContainerProperties.Property(name =
> "openejb.testing.start-cdi-contexts", value = "false"))
> @Classes(cdi = true, innerClassesAsBean = true)
> @RunWith(ApplicationComposer.class)
> public class ScopesRuleTest {
> public static class Foo {
> public void touch() {}
> }
>
> @Rule
> public final ScopesRule rule = new ScopesRule();
>
> @Inject
> private BeanManager beanManager;
>
> @Test
> public void scopeDoesNotExist() {
> Assert.assertFalse(beanManager.getContext(
> RequestScoped.class).isActive());
> }
>
> @Test
> @CdiScopes(RequestScoped.class)
> public void scopeExists() {
> Assert.assertTrue(beanManager.getContext(
> RequestScoped.class).isActive());
> }
> }
>
> The scopes should not be started but the first test gives an assertion
> error (so the RequestContext is well started although that's not what I
> wanted) and the second one gives a NPE:
>
> java.lang.NullPointerException
> at org.apache.openejb.util.AppFinder.findAppContextOrWeb(
> AppFinder.java:28)
> at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(
> ThreadSingletonServiceImpl.java:287)
> at org.apache.openejb.cdi.ThreadSingletonServiceImpl.getContext(
> ThreadSingletonServiceImpl.java:267)
> at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(
> ThreadSingletonServiceImpl.java:327)
> at org.apache.openejb.cdi.ThreadSingletonServiceImpl.get(
> ThreadSingletonServiceImpl.java:64)
> at org.apache.webbeans.config.WebBeansFinder.getSingletonInstance(
> WebBeansFinder.java:51)
> at org.apache.webbeans.config.WebBeansContext.getInstance(
> WebBeansContext.java:185)
> at org.apache.webbeans.config.WebBeansContext.currentInstance(
> WebBeansContext.java:203)
> at org.apache.openejb.junit.ScopesRule$1.evaluate(ScopesRule.java:42)
> at org.junit.rules.RunRules.evaluate(RunRules.java:20)
>
> If I remove the first test, the second one passes.
>
> Is this a bug?
>
> Thanks,
>
> Xavier