Example of Resin 4's JUnit support for unit testing with CDI

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "=JUnit: No Injection= ====foo/qa/MyTest.java==== <pre> package qa; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.JUnitCor...")
 
 
Line 1: Line 1:
 
=JUnit: No Injection=
 
=JUnit: No Injection=
====foo/qa/MyTest.java====
+
====foo/qa/MyTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 34: Line 34:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: [failAdd(qa.MyTest): ]
+
results: [failAdd(qa.MyTest): ]
</pre>
+
</pre>
  
 
=JUnit: Child Context Class-Loader=
 
=JUnit: Child Context Class-Loader=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/SubBean.java====
+
====foo/qa/SubBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public class SubBean {
 
  public class SubBean {
Line 56: Line 56:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MyTest.java====
+
====foo/qa/MyTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 96: Line 96:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state: EnvironmentClassLoader[resin-context]
 
  state: EnvironmentClassLoader[resin-context]
</pre>
+
</pre>
  
 
=JUnit: Default Beans XML=
 
=JUnit: Default Beans XML=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/SubBean.java====
+
====foo/qa/SubBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public class SubBean {
 
  public class SubBean {
Line 119: Line 119:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MyTest.java====
+
====foo/qa/MyTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 159: Line 159:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state: SubBean[]
 
  state: SubBean[]
</pre>
+
</pre>
  
 
=JUnit: Specifying Multiple Beans XML, Including Default Beans XML=
 
=JUnit: Specifying Multiple Beans XML, Including Default Beans XML=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/beans-test.xml====
+
====foo/beans-test.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 191: Line 191:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 204: Line 204:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 220: Line 220:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 237: Line 237:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 256: Line 256:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 300: Line 300:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state:
 
  state:
 
   hit: intercept() mock-hit()
 
   hit: intercept() mock-hit()
</pre>
+
</pre>
  
 
=JUnit: Overriding/Replacing Default beans.xml=
 
=JUnit: Overriding/Replacing Default beans.xml=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/beans-test.xml====
+
====foo/beans-test.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 333: Line 333:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 346: Line 346:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 362: Line 362:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 379: Line 379:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 398: Line 398:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 442: Line 442:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state:
 
  state:
 
   hit: mock-hit()
 
   hit: mock-hit()
</pre>
+
</pre>
  
 
=JUnit: Specifying Multiple beans.xml, Excluding the Default=
 
=JUnit: Specifying Multiple beans.xml, Excluding the Default=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/beans-test-mock.xml====
+
====foo/beans-test-mock.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/beans-test-interceptor.xml====
+
====foo/beans-test-interceptor.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor2</class>
 
     <class>qa.TestInterceptor2</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 483: Line 483:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 497: Line 497:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 514: Line 514:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 531: Line 531:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding2.java====
+
====foo/qa/TestInterceptorBinding2.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 548: Line 548:
 
  public @interface TestInterceptorBinding2 {
 
  public @interface TestInterceptorBinding2 {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 567: Line 567:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor2.java====
+
====foo/qa/TestInterceptor2.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 586: Line 586:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 630: Line 630:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state:
 
  state:
 
   hit: intercept2() mock-hit()
 
   hit: intercept2() mock-hit()
</pre>
+
</pre>
  
 
=JUnit: Explicitly Specifying Beans XML Files in the Class-Path=
 
=JUnit: Explicitly Specifying Beans XML Files in the Class-Path=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/beans-test.xml====
+
====foo/beans-test.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 663: Line 663:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 676: Line 676:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 692: Line 692:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 709: Line 709:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 728: Line 728:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 773: Line 773:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state: intercept() mock-hit()
 
  state: intercept() mock-hit()
</pre>
+
</pre>
  
 
=JUnit: Specifying Beans XML File in the File-System=
 
=JUnit: Specifying Beans XML File in the File-System=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo-resources/beans-test.xml====
+
====foo-resources/beans-test.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 802: Line 802:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 815: Line 815:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 832: Line 832:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 851: Line 851:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 895: Line 895:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state: intercept() hit()
 
  state: intercept() hit()
</pre>
+
</pre>
  
 
=JUnit: Specifying Multiple Beans XML Files in the File-System=
 
=JUnit: Specifying Multiple Beans XML Files in the File-System=
====foo-resources/beans-interceptors.xml====
+
====foo-resources/beans-interceptors.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo-resources/beans-alternatives.xml====
+
====foo-resources/beans-alternatives.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/beans.xml====
+
====foo/qa/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 932: Line 932:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 945: Line 945:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 961: Line 961:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 978: Line 978:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 997: Line 997:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 1,042: Line 1,042:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state:
 
  state:
 
   hit: intercept() mock-hit()
 
   hit: intercept() mock-hit()
</pre>
+
</pre>
  
 
=JUnit: Specifying Beans XML Files in the File-System and Class-Path=
 
=JUnit: Specifying Beans XML Files in the File-System and Class-Path=
====foo/META-INF/beans.xml====
+
====foo/META-INF/beans.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <interceptors>
 
   <interceptors>
 
     <class>qa.TestInterceptor</class>
 
     <class>qa.TestInterceptor</class>
 
   </interceptors>
 
   </interceptors>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo-resources/beans-test.xml====
+
====foo-resources/beans-test.xml====
<pre>
+
<pre>
<beans xmlns="http://java.sun.com/xml/ns/javaee">
+
<beans xmlns="http://java.sun.com/xml/ns/javaee">
 
   <alternatives>
 
   <alternatives>
 
     <class>qa.MockTestBean</class>
 
     <class>qa.MockTestBean</class>
 
   </alternatives>
 
   </alternatives>
 
  </beans>
 
  </beans>
</pre>
+
</pre>
====foo/qa/Test.java====
+
====foo/qa/Test.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  public interface Test {
 
  public interface Test {
Line 1,075: Line 1,075:
 
   public String hit();
 
   public String hit();
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestBean.java====
+
====foo/qa/TestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  @TestInterceptorBinding
 
  @TestInterceptorBinding
Line 1,088: Line 1,088:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/MockTestBean.java====
+
====foo/qa/MockTestBean.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.enterprise.inject.Alternative;
 
  import javax.enterprise.inject.Alternative;
Line 1,104: Line 1,104:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptorBinding.java====
+
====foo/qa/TestInterceptorBinding.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import java.lang.annotation.ElementType;
 
  import java.lang.annotation.ElementType;
Line 1,121: Line 1,121:
 
  public @interface TestInterceptorBinding {
 
  public @interface TestInterceptorBinding {
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/TestInterceptor.java====
+
====foo/qa/TestInterceptor.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import javax.interceptor.AroundInvoke;
 
  import javax.interceptor.AroundInvoke;
Line 1,140: Line 1,140:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====foo/qa/JUnitTest.java====
+
====foo/qa/JUnitTest.java====
<pre>
+
<pre>
package qa;
+
package qa;
 
   
 
   
 
  import static org.junit.Assert.assertTrue;
 
  import static org.junit.Assert.assertTrue;
Line 1,185: Line 1,185:
 
   }
 
   }
 
  }
 
  }
</pre>
+
</pre>
====Output====
+
====Output====
<pre>
+
<pre>
results: []
+
results: []
 
  state: intercept() mock-hit()
 
  state: intercept() mock-hit()
</pre>
+
</pre>

Latest revision as of 00:00, 9 June 2012

Contents

JUnit: No Injection

foo/qa/MyTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 public class MyTest {
   @Test
   public void simpleAdd()
   {
     assertTrue(1 + 1 == 2);
   }
 
   @Test
   public void failAdd()
   {
     assertTrue(1 + 1 == 3);
   }
 
   public static void main(String[] args)
   {
     Result result = JUnitCore.runClasses(MyTest.class);
 
     System.out.print("results: " + result.getFailures());
   }
 }

Output

results: [failAdd(qa.MyTest): ]

JUnit: Child Context Class-Loader

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
 </beans>

foo/qa/SubBean.java

package qa;
 
 public class SubBean {
   public String toString()
   {
     return "SubBean[]";
   }
 }

foo/qa/MyTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 public class MyTest {
   @Inject
   SubBean _subBean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     _state += Thread.currentThread().getContextClassLoader();
 
     assertTrue(_subBean != null);
   }
 
   public static void main(String[] args)
   {
     Result result = JUnitCore.runClasses(MyTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state: " + _state);
   }
 }

Output

results: []
 state: EnvironmentClassLoader[resin-context]

JUnit: Default Beans XML

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
 </beans>

foo/qa/SubBean.java

package qa;
 
 public class SubBean {
   public String toString()
   {
     return "SubBean[]";
   }
 }

foo/qa/MyTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 public class MyTest {
   @Inject
   SubBean _subBean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     _state += _subBean;
 
     assertTrue(_subBean != null);
   }
 
   public static void main(String[] args)
   {
     Result result = JUnitCore.runClasses(MyTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state: " + _state);
   }
 }

Output

results: []
 state: SubBean[]

JUnit: Specifying Multiple Beans XML, Including Default Beans XML

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo/beans-test.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return " intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "META-INF/beans.xml", "beans-test.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += "\n  hit:" + _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state:" + _state);
   }
 }

Output

results: []
 state:
   hit: intercept() mock-hit()

JUnit: Overriding/Replacing Default beans.xml

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo/beans-test.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return " intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "beans-test.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += "\n  hit:" + _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state:" + _state);
   }
 }

Output

results: []
 state:
   hit: mock-hit()

JUnit: Specifying Multiple beans.xml, Excluding the Default

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo/beans-test-mock.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/beans-test-interceptor.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor2</class>
   </interceptors>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 @TestInterceptorBinding2
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 @TestInterceptorBinding2
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptorBinding2.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding2 {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return " intercept()" + context.proceed();
   }
 }

foo/qa/TestInterceptor2.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding2
 public class TestInterceptor2 {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return " intercept2()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "beans-test-mock.xml", "beans-test-interceptor.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += "\n  hit:" + _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state:" + _state);
   }
 }

Output

results: []
 state:
   hit: intercept2() mock-hit()

JUnit: Explicitly Specifying Beans XML Files in the Class-Path

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo/beans-test.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return "intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "classpath:META-INF/beans.xml", 
     "classpath:beans-test.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state: " + _state);
   }
 }

Output

results: []
 state: intercept() mock-hit()

JUnit: Specifying Beans XML File in the File-System

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
 </beans>

foo-resources/beans-test.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return "intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "file:/tmp/caucho/qa/foo-resources/beans-test.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state: " + _state);
   }
 }

Output

results: []
 state: intercept() hit()

JUnit: Specifying Multiple Beans XML Files in the File-System

foo-resources/beans-interceptors.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo-resources/beans-alternatives.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/qa/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return " intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "file:/tmp/caucho/qa/foo-resources/beans-alternatives.xml", 
     "file:/tmp/caucho/qa/foo-resources/beans-interceptors.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += "\n  hit:" + _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state:" + _state);
   }
 }

Output

results: []
 state:
   hit: intercept() mock-hit()

JUnit: Specifying Beans XML Files in the File-System and Class-Path

foo/META-INF/beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <interceptors>
     <class>qa.TestInterceptor</class>
   </interceptors>
 </beans>

foo-resources/beans-test.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee">
   <alternatives>
     <class>qa.MockTestBean</class>
   </alternatives>
 </beans>

foo/qa/Test.java

package qa;
 
 public interface Test {
 
   public String hit();
 }

foo/qa/TestBean.java

package qa;
 
 @TestInterceptorBinding
 public class TestBean implements Test {
 
   public String hit()
   {
     return " hit()";
   }
 }

foo/qa/MockTestBean.java

package qa;
 
 import javax.enterprise.inject.Alternative;
 
 @Alternative
 @TestInterceptorBinding
 public class MockTestBean implements Test {
 
   public String hit()
   {
     return " mock-hit()";
   }
 }

foo/qa/TestInterceptorBinding.java

package qa;
 
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 import javax.interceptor.InterceptorBinding;
 
 @InterceptorBinding
 @Target( { ElementType.TYPE })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface TestInterceptorBinding {
 }

foo/qa/TestInterceptor.java

package qa;
 
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
 import javax.interceptor.InvocationContext;
 
 @Interceptor
 @TestInterceptorBinding
 public class TestInterceptor {
 
   @AroundInvoke
   public Object intercept(InvocationContext context) throws Exception
   {
     return "intercept()" + context.proceed();
   }
 }

foo/qa/JUnitTest.java

package qa;
 
 import static org.junit.Assert.assertTrue;
 
 import javax.inject.Inject;
 
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 import org.junit.runner.RunWith;
 
 import com.caucho.junit.ResinBeanConfiguration;
 import com.caucho.junit.ResinBeanContainerRunner;
 
 @RunWith(ResinBeanContainerRunner.class)
 @ResinBeanConfiguration(beansXml = { "META-INF/beans.xml", 
     "file:/tmp/caucho/qa/foo-resources/beans-test.xml" })
 public class JUnitTest {
   @Inject
   private qa.Test _bean;
 
   static String _state = "";
 
   @Test
   public void test()
   {
     assertTrue(_bean != null);
 
     _state += _bean.hit();
   }
 
   public static void main(String [] arguments)
   {
     JUnitCore jUnit = new JUnitCore();
 
     Result result = JUnitCore.runClasses(JUnitTest.class);
 
     System.out.println("results: " + result.getFailures());
 
     System.out.print("state: " + _state);
   }
 }

Output

results: []
 state: intercept() mock-hit()
Personal tools
TOOLBOX
LANGUAGES