deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r1594951 - /deltaspike/site/trunk/content/test-control.mdtext
Date Thu, 15 May 2014 15:36:13 GMT
Author: gpetracek
Date: Thu May 15 15:36:12 2014
New Revision: 1594951

URL: http://svn.apache.org/r1594951
Log:
updated content

Modified:
    deltaspike/site/trunk/content/test-control.mdtext

Modified: deltaspike/site/trunk/content/test-control.mdtext
URL: http://svn.apache.org/viewvc/deltaspike/site/trunk/content/test-control.mdtext?rev=1594951&r1=1594950&r2=1594951&view=diff
==============================================================================
--- deltaspike/site/trunk/content/test-control.mdtext (original)
+++ deltaspike/site/trunk/content/test-control.mdtext Thu May 15 15:36:12 2014
@@ -144,6 +144,158 @@ If you are using OpenWebBeans as CDI imp
 
 # Optional Integrations
 
+## Mock Frameworks
+
+With v0.8+ it's possible to mock CDI-Beans. Usually @Exclude (+ project-stage) is enough,
however, for some cases mocked beans might be easier. Therefore it's possible to create (mock-)instances
manually or via a mocking framework and add them e.g. via `DynamicMockManager`.
+
+If you need dependency-injection in the mocked instances, you can use `BeanProvider.injectFields(myMockedBean);`.
+
+    :::java
+    @RunWith(CdiTestRunner.class)
+    public class MockedRequestScopedBeanTest
+    {
+        @Inject
+        private RequestScopedBean requestScopedBean;
+
+        @Inject
+        private DynamicMockManager mockManager;
+
+        @Test
+        public void manualMock()
+        {
+            mockManager.addMock(new RequestScopedBean() {
+                @Override
+                public int getCount()
+                {
+                    return 7;
+                }
+            });
+
+            Assert.assertEquals(7, requestScopedBean.getCount());
+            requestScopedBean.increaseCount();
+            Assert.assertEquals(7, requestScopedBean.getCount());
+        }
+    }
+    
+    @RequestScoped
+    public class RequestScopedBean
+    {
+        private int count = 0;
+
+        public int getCount()
+        {
+            return count;
+        }
+
+        public void increaseCount()
+        {
+            this.count++;
+        }
+    }
+
+Using a mocking framework makes no difference for adding the mock. E.g. via Mockito:
+
+    :::java
+    @RunWith(CdiTestRunner.class)
+    public class MockitoMockedRequestScopedBeanTest
+    {
+        @Inject
+        private RequestScopedBean requestScopedBean;
+
+        @Inject
+        private DynamicMockManager mockManager;
+
+        @Test
+        public void mockitoMockAsCdiBean()
+        {
+            RequestScopedBean mockedRequestScopedBean = mock(RequestScopedBean.class);
+            when(mockedRequestScopedBean.getCount()).thenReturn(7);
+            mockManager.addMock(mockedRequestScopedBean);
+
+            Assert.assertEquals(7, requestScopedBean.getCount());
+            requestScopedBean.increaseCount();
+            Assert.assertEquals(7, requestScopedBean.getCount());
+        }
+    }
+
+
+Since CDI implementations like OpenWebBeans use a lot of optimizations, it's required to
handle mocks for application-scoped beans differently - e.g.:
+However, `ApplicationMockManager` can be used for adding all mocks, if they should be active
for the lifetime of the CDI-container.
+
+    :::java
+    @RunWith(CdiTestRunner.class)
+    public class MockedApplicationScopedBeanTest
+    {
+        @Inject
+        private ApplicationScopedBean applicationScopedBean;
+
+        @BeforeClass
+        public static void init()
+        {
+            ApplicationMockManager applicationMockManager = BeanProvider.getContextualReference(ApplicationMockManager.class);
+            applicationMockManager.addMock(new MockedApplicationScopedBean());
+        }
+
+        @Test
+        public void manualMock()
+        {
+            Assert.assertEquals(14, applicationScopedBean.getCount());
+            applicationScopedBean.increaseCount();
+            Assert.assertEquals(14, applicationScopedBean.getCount());
+        }
+    }
+    
+    @ApplicationScoped
+    public class ApplicationScopedBean
+    {
+        private int count = 0;
+
+        public int getCount()
+        {
+            return count;
+        }
+    
+        public void increaseCount()
+        {
+            this.count++;
+        }
+    }
+
+It's also possible to mock qualified beans. Just add the literal-instance(s) as additional
parameter(s) - e.g.:
+
+    :::java
+    @RunWith(CdiTestRunner.class)
+    public class MockedQualifiedBeanTest
+    {
+        @Inject
+        @MyQualifier
+        private QualifiedBean qualifiedBean;
+
+        @Inject
+        private DynamicMockManager mockManager;
+
+        @Test
+        public void manualMockWithQualifier()
+        {
+            mockManager.addMock(new QualifiedBean() {
+                @Override
+                public int getCount()
+                {
+                    return 21;
+                }
+            }, AnnotationInstanceProvider.of(MyQualifier.class));
+
+            Assert.assertEquals(21, qualifiedBean.getCount());
+            qualifiedBean.increaseCount();
+            Assert.assertEquals(21, qualifiedBean.getCount());
+        }
+    }
+
+In some cases it's needed to use `@javax.enterprise.inject.Typed`. Mocking such typed beans
can result in an `AmbiguousResolutionException`.
+Therefore it's needed to exclude the mocked implementation via `@Exclude` or `@Typed()` (or
a parametrized constructor) and specify the target-type via `@TypedMock`.
+
+
+
 ## JSF (via MyFaces-Test)
 
 add:
@@ -181,4 +333,4 @@ Currently DeltaSpike provides:
 
  * MockedJsf2TestContainer (integration with MyFaces-Test)
 
-[TODO]
+[TODO]
\ No newline at end of file



Mime
View raw message