8 Replies Latest reply: Jun 18, 2012 6:21 AM by Luca Cavagnoli RSS

mock-contexts-extension

Bernard Labno Master

Hi guys,

 

I've created Arquillian extension that enables Conversation and ViewScoped contexts for your tests. It can also activate FacesContext if you provide mock for it.

 

Source code:

https://github.com/blabno/mock-contexts-extension/

 

Blog posts:

http://blog.it-crowd.com.pl/2012/04/mock-contexts-for-arquillian.html

http://blog.it-crowd.com.pl/2012/05/mock-facescontext-in-arquillian.html

 

I'm looking forward to your feedback.

 

Aslak, what do you think about including this into arquillian repo?

  • 1. Re: mock-contexts-extension
    Luca Cavagnoli Newbie

    Thanks for your work, the ability to mock FacesContext is really needed!

     

    I added your repository and dependencies to the pom.xml of the JBoss Central's the Java EE Web Peoject in JBoss Developer Studio.

     

    I get the following exception when building:

     

    eclipse.buildId=unknown

    java.version=1.6.0_31

    java.vendor=Sun Microsystems Inc.

    BootLoader constants: OS=win32, ARCH=x86, WS=win32, NL=en_GB

    Framework arguments:  -product com.jboss.jbds.product.product

    Command-line arguments:  -os win32 -ws win32 -arch x86 -product com.jboss.jbds.product.product

     

    Error

    Wed Jun 06 10:04:48 CEST 2012

    Errors running builder 'Java Builder' on project 'javaeeWebProject'.

     

    java.lang.AssertionError: Couldn't find a type mirror for class interface org.hibernate.validator.group.DefaultGroupSequenceProvider

        at org.hibernate.validator.ap.util.AnnotationApiHelper.getMirrorForNonArrayType(AnnotationApiHelper.java:166)

        at org.hibernate.validator.ap.util.AnnotationApiHelper.getMirrorForType(AnnotationApiHelper.java:147)

        at org.hibernate.validator.ap.checks.GroupSequenceProviderCheck.<init>(GroupSequenceProviderCheck.java:63)

        at org.hibernate.validator.ap.checks.ConstraintCheckFactory.<init>(ConstraintCheckFactory.java:125)

        at org.hibernate.validator.ap.ConstraintAnnotationVisitor.<init>(ConstraintAnnotationVisitor.java:68)

        at org.hibernate.validator.ap.ConstraintValidationProcessor.process(ConstraintValidationProcessor.java:101)

        at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139)

        at org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:121)

        at org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159)

        at org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager.processAnnotations(IdeAnnotationProcessorManager.java:134)

        at org.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:813)

        at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:432)

        at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:364)

        at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.compile(BatchImageBuilder.java:178)

        at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:301)

        at org.eclipse.jdt.internal.core.builder.BatchImageBuilder.build(BatchImageBuilder.java:60)

        at org.eclipse.jdt.internal.core.builder.JavaBuilder.buildAll(JavaBuilder.java:254)

        at org.eclipse.jdt.internal.core.builder.JavaBuilder.build(JavaBuilder.java:178)

        at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)

        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)

        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)

        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)

        at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)

        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)

        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)

        at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)

        at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)

        at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)

        at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)

        at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

  • 2. Re: mock-contexts-extension
    Bernard Labno Master

    I have no idea what hibernate has to do with mock-contexts-extension, sorry. Maybe you can post your pom and try building the project, or running the tests from maven.

  • 3. Re: mock-contexts-extension
    Luca Cavagnoli Newbie

    I'll attach the pom.xml.

  • 4. Re: mock-contexts-extension
    Bernard Labno Master

    jars to local repo remove reference to my artifactory and see if problem persists.I don't know what's wrong. Please, after grabbing mock-contexts-extension jars to your local repo, remove references to my atifactory and see if problem persits.

    If so then it's problem with some hibernate jars, which should not be caused by my extension nor artifactory.

  • 5. Re: mock-contexts-extension
    Luca Cavagnoli Newbie

    Problem fixed. I reopen JBIDE and now it builds perfectly. Go figure..

  • 6. Re: mock-contexts-extension
    Luca Cavagnoli Newbie

    I'm trying out the @ConversationScopeRequired annotation, but the conversation context doesn't get created.

     

     

    Here is my conversational bean:

     

    package org.lucaster.controller;

     

    import java.io.Serializable;

     

    import javax.enterprise.context.Conversation;

    import javax.enterprise.context.ConversationScoped;

    import javax.inject.Inject;

    import javax.inject.Named;

     

    @Named

    @ConversationScoped

    public class ConvBean implements Serializable {

     

        private static final long serialVersionUID = 3071980748674417078L;

     

        private int counter;

     

        @Inject

        Conversation conversation;

     

        //@PostConstruct

        public void start() {

            if (conversation.isTransient()) {

                conversation.begin();

            }

        }

     

        public void increment() {

            counter++;

        }

     

        public void end() {

            if (!conversation.isTransient()) {

                conversation.end();

            }

        }

     

        public int getCounter() {

            return counter;

        }

     

        public void setCounter(int counter) {

            this.counter = counter;

        }

    }

     

     

    Here is my test:

     

    package org.lucaster.controller;

     

    import static org.junit.Assert.assertTrue;

    import static org.mockito.Mockito.mock;

     

    import javax.faces.context.FacesContext;

    import javax.inject.Inject;

     

    import org.jboss.arquillian.container.test.api.Deployment;

    import org.jboss.arquillian.junit.Arquillian;

    import org.jboss.shrinkwrap.api.Archive;

    import org.jboss.shrinkwrap.api.GenericArchive;

    import org.jboss.shrinkwrap.api.ShrinkWrap;

    import org.jboss.shrinkwrap.api.asset.EmptyAsset;

    import org.jboss.shrinkwrap.api.spec.WebArchive;

    import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;

    import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;

    import org.junit.Test;

    import org.junit.runner.RunWith;

     

    import pl.com.it_crowd.arquillian.mock_contexts.ConversationScopeRequired;

    import pl.com.it_crowd.arquillian.mock_contexts.FacesContextRequired;

    import pl.com.it_crowd.arquillian.mock_contexts.MockFacesContextProducer;

     

    @RunWith(Arquillian.class)

    public class ConvBeanTest {

        @Deployment

        public static Archive<?> createTestArchive() {

            return ShrinkWrap

                    .create(WebArchive.class, "ConvBeanTest.war")

                    .addClasses(ConvBean.class)

                    .addAsResource("META-INF/test-persistence.xml",

                            "META-INF/persistence.xml")

                    .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")

                    .addAsWebInfResource("test-ds.xml", "test-ds.xml")

                    //

                    // I had to add these to avoid ClassNotFountException org\mockito\Mockito

                    // enen though Mockito dependencies are configured in pom.xml

                    //

                    .addAsLibraries(

                            DependencyResolvers.use(MavenDependencyResolver.class)

                                    .artifact("org.mockito:mockito-all:1.9.0")

                                    .resolveAs(GenericArchive.class))

                    .addAsLibraries(

                            DependencyResolvers.use(MavenDependencyResolver.class)

                                    .artifact("org.mockito:mockito-core:1.9.0")

                                    .resolveAs(GenericArchive.class));

        }

     

        @Inject

        private ConvBean convBean;

     

        private FacesContext facesContextMock;

     

        @MockFacesContextProducer

        public FacesContext mockFacesContext() {

            if (facesContextMock == null)

                facesContextMock = mock(FacesContext.class);

            return facesContextMock;

        }

     

        @ConversationScopeRequired

        @FacesContextRequired

        @Test

        public void incrementTest() throws Exception {

            convBean.start();

            assertTrue(0 == convBean.getCounter());

            convBean.increment();

            assertTrue(1 == convBean.getCounter());

        }

     

    }

     

     

    Here is the test stack trace:

     

    javax.enterprise.context.ContextNotActiveException: Conversation Context not active when method called on conversation Transient conversation

        at org.jboss.weld.context.conversation.ConversationImpl.verifyConversationContextActive(ConversationImpl.java:197)

        at org.jboss.weld.context.conversation.ConversationImpl.touch(ConversationImpl.java:159)

        at org.jboss.weld.context.conversation.ConversationImpl.<init>(ConversationImpl.java:72)

        at org.jboss.weld.bean.builtin.ConversationBean.create(ConversationBean.java:48)

        at org.jboss.weld.bean.builtin.ConversationBean.create(ConversationBean.java:17)

        at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:107)

        at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:90)

        at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:104)

        at org.jboss.weld.proxies.Conversation$923794727$Proxy$_$$_WeldClientProxy.isTransient(Conversation$923794727$Proxy$_$$_WeldClientProxy.java)

        at org.lucaster.controller.ConvBean.start(ConvBean.java:23)

        at org.lucaster.controller.ConvBean$Proxy$_$$_WeldClientProxy.start(ConvBean$Proxy$_$$_WeldClientProxy.java)

        at org.lucaster.controller.ConvBeanTest.incrementTest(ConvBeanTest.java:66)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

        at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:270)

        at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

        at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

        at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

        at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:134)

        at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:114)

        at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)

        at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

        at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

        at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

        at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:89)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

        at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

        at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

        at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

        at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)

        at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)

        at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:134)

        at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:111)

        at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:263)

        at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:226)

        at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)

        at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

        at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:240)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)

        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

        at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:185)

        at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)

        at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

        at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199)

        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

        at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)

        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)

        at org.junit.runner.JUnitCore.run(JUnitCore.java:136)

        at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:65)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126)

        at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:90)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

        at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)

        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

        at java.lang.Thread.run(Thread.java:662)

     

     

    I'm building my app using the jboss-javaee6-webapp artifact.

    Environment: JBoss AS 7.1.1.Final

  • 7. Re: mock-contexts-extension
    Bernard Labno Master

    Read this blog post: http://blog.it-crowd.com.pl/2012/06/mock-conversation.html

     

    Message was edited by: Bernard Labno

  • 8. Re: mock-contexts-extension
    Luca Cavagnoli Newbie

    Yes, the test run configuration has all the Maven Dependencies in its classpath, which include mock-context-extension.

     

    I also used mock-contexts-extension to mock the FacesContext of a @RequestScoped bean in another test, and that worked out fine (except faces messages are not kept).