Uploaded image for project: 'Arquillian'
  1. Arquillian
  2. ARQ-2034

Test enrichment and JUnit @Rule do not harmonize

XMLWordPrintable

    • Workaround Exists
    • Hide

      The field is marked as committers-only, but I found a work-around, so I thought I'd share it here.

      package foo;
      
      import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
      import org.jboss.arquillian.junit.RulesEnricher;
      
      public class RulesEnricherExtension implements RemoteLoadableExtension
      {
         @Override
         public void register(ExtensionBuilder builder)
         {
            builder.observer(RulesEnricher.class);
         }
      }
      

      And in META-INF/services/org.jboss.arquillian.container.test.spi.RemoteLoadableExtension as content foo.RulesEnricherExtension.

      Now the injection into @Rule instances works like a charm.

      Show
      The field is marked as committers-only, but I found a work-around, so I thought I'd share it here. package foo; import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension; import org.jboss.arquillian.junit.RulesEnricher; public class RulesEnricherExtension implements RemoteLoadableExtension { @Override public void register(ExtensionBuilder builder) { builder.observer(RulesEnricher.class); } } And in META-INF/services/org.jboss.arquillian.container.test.spi.RemoteLoadableExtension as content foo.RulesEnricherExtension . Now the injection into @Rule instances works like a charm.

      We are starting with Arquillian tests on a remote WildFly instance.

      I tried to write a JUnit @Rule, that can be used to make a login call for all test methods automatically, but I was not yet able to make it work, as I need an injected @Ejb instance for this.

      I tried to add @EJB private InterfaceLoginManagement interfaceLoginManagement; to the TestRule implementation, but it stays null. I've read at ARQ-1954 that this should probably work, but unfortunately it is not, at least not with the remote container runner. org.jboss.arquillian.junit.extension.JUnitCoreExtension that should register org.jboss.arquillian.junit.extension.UpdateTestResultBeforeAfter and org.jboss.arquillian.junit.RulesEnricher is not even loaded on the container side. Maybe the META-INF/services/ file is missing?

      Then I tried to simply give the injected instance to the rule from the test class like

      @EJB private InterfaceLoginManagement interfaceLoginManagement;
      @Rule private LoginRule loginRule = new LoginRule(interfaceLoginManagement);
      

      or even

      @EJB private InterfaceLoginManagement interfaceLoginManagement;
      @Rule public LoginRule getLoginRule() {
         return new LoginRule(() -> {
            return interfaceLoginManagement;
         });
      }
      

      with the LoginRule receiving a Supplier<InterfaceLoginManagement> and call it as late as possible, i. e. in the rules apply() method. But even this is still too early as the EJB is still null and the test enrichers are only run between the @Rule applying and @Before methods.

      So I'd like to suggest two things:

      1. Fix the implementation of ARQ-1954 if it should work on container side which I assume it should
      2. Make the test enrichments earlier in the lifecycle, i. e. before the @Rule s are being applied, or rather even before the @Rule methods are called, so that you at least can use the injected instances in the @Rule annotated methods to forward them to a rule. This is necessary even if top 1 is taken care of, because you might want to use a 3rd Party rule the code of which you cannot change.

              mjobanek Matous Jobanek
              Vampire0 Björn Kautler (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: