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()