You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2013/06/07 18:40:38 UTC

git commit: ISIS-428: improving JUnitRuleMockery2

Updated Branches:
  refs/heads/master ee10fcfbb -> 57706bfb8


ISIS-428: improving JUnitRuleMockery2


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/57706bfb
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/57706bfb
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/57706bfb

Branch: refs/heads/master
Commit: 57706bfb84e82da56ebd6c5038fa1e9388b8b331
Parents: ee10fcf
Author: Dan Haywood <da...@apache.org>
Authored: Fri Jun 7 17:27:32 2013 +0100
Committer: Dan Haywood <da...@apache.org>
Committed: Fri Jun 7 17:27:32 2013 +0100

----------------------------------------------------------------------
 .../jmocking/JUnitRuleMockery2.java                |   28 ++++++++++-----
 ..._autoWiring_constructorInjection_happyCase.java |   10 +++---
 ...2Test_autoWiring_setterInjection_happyCase.java |    9 +++--
 ...RuleMockery2Test_mockAnnotatedWithAllowing.java |    9 +++--
 ...RuleMockery2Test_mockAnnotatedWithChecking.java |    9 +++--
 ...RuleMockery2Test_mockAnnotatedWithIgnoring.java |    9 +++--
 ...kery2Test_mockAnnotatedWithNever_happyCase.java |    9 +++--
 ...ockery2Test_mockAnnotatedWithNever_sadCase.java |    9 +++--
 ...ockery2Test_mockAnnotatedWithOne_happyCase.java |    9 +++--
 ...eMockery2Test_mockAnnotatedWithOne_sadCase.java |    9 +++--
 10 files changed, 64 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2.java b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2.java
index 1c8c278..f4ac728 100644
--- a/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2.java
+++ b/core/unittestsupport/src/main/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2.java
@@ -150,17 +150,26 @@ public class JUnitRuleMockery2 extends JUnit4Mockery implements MethodRule {
                 assertIsSatisfied();
             }
 
-            private void prepare(final Object target) {
+            private void prepare(final Object target) throws IllegalAccessException {
                 final List<Field> allFields = AllDeclaredFields.in(target.getClass());
                 assertOnlyOneJMockContextIn(allFields);
                 List<Object> mocks = fillInAutoMocks(target, allFields);
-                cutType = locateClassUnderTestIfAny(allFields);
-                if(cutType != null) {
-	                for (Object mock : mocks) {
-	                	container.addComponent(mock);
-	                }
-	                container.addComponent(cutType);
+                Field cutField = locateClassUnderTestFieldIfAny(allFields);
+                if (cutField != null) {
+                    cutType = cutField.getType();
+                    for (Object mock : mocks) {
+                        container.addComponent(mock);
+                    }
+                    container.addComponent(cutType);
+                    
+                    final Object cut = container.getComponent(cutType);
+                    cutField.setAccessible(true);
+                    cutField.set(target, cut);
+
+                } else {
+                    cutType = null;
                 }
+                
             }
 
             private void assertOnlyOneJMockContextIn(final List<Field> allFields) {
@@ -175,7 +184,8 @@ public class JUnitRuleMockery2 extends JUnit4Mockery implements MethodRule {
                 }
             }
 
-            private Class<?> locateClassUnderTestIfAny(final List<Field> allFields) {
+
+            protected Field locateClassUnderTestFieldIfAny(final List<Field> allFields) {
                 Field cutField = null;
                 for (final Field field : allFields) {
                 	if(field.getAnnotation(ClassUnderTest.class) != null) {
@@ -185,7 +195,7 @@ public class JUnitRuleMockery2 extends JUnit4Mockery implements MethodRule {
                         cutField = field;
                     }
                 }
-                return cutField != null? cutField.getType(): null;
+                return cutField;
             }
 
             private List<Object> fillInAutoMocks(final Object target, final List<Field> allFields) {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase.java
index 4a58645..f8c5a27 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase.java
@@ -25,7 +25,6 @@ import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
 import org.jmock.auto.Mock;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -43,10 +42,11 @@ public class JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Test
     public void wiring() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase.java
index c719ea3..705b2f7 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase.java
@@ -43,10 +43,11 @@ public class JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase {
     @ClassUnderTest
 	private CollaboratingUsingSetterInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = context.getClassUnderTest();
+//	}
     
     @Test
     public void wiring() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithAllowing.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithAllowing.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithAllowing.java
index 826aefb..3b820b9 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithAllowing.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithAllowing.java
@@ -40,10 +40,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithAllowing {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithChecking.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithChecking.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithChecking.java
index 5289308..a1d3b99 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithChecking.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithChecking.java
@@ -49,10 +49,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithChecking {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithIgnoring.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithIgnoring.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithIgnoring.java
index 4a34b11..276d0a9 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithIgnoring.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithIgnoring.java
@@ -40,10 +40,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithIgnoring {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = context.getClassUnderTest();
+//	}
     
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase.java
index 17c7fa6..5f3bb86 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase.java
@@ -40,10 +40,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithNever_happyCase {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase.java
index dc1ed21..798ee3f 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase.java
@@ -45,10 +45,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithNever_sadCase {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = context.getClassUnderTest();
+//	}
 
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase.java
index 42b8950..871bafc 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase.java
@@ -40,10 +40,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithOne_happyCase {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Test
     public void invocationOnCollaboratorIsIgnored() {

http://git-wip-us.apache.org/repos/asf/isis/blob/57706bfb/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase.java
----------------------------------------------------------------------
diff --git a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase.java b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase.java
index 922e934..862337d 100644
--- a/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase.java
+++ b/core/unittestsupport/src/test/java/org/apache/isis/core/unittestsupport/jmocking/JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase.java
@@ -41,10 +41,11 @@ public class JUnitRuleMockery2Test_mockAnnotatedWithOne_sadCase {
     @ClassUnderTest
 	private CollaboratingUsingConstructorInjection collaborating;
 
-    @Before
-	public void setUp() throws Exception {
-    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
-	}
+    // no longer necessary :-)
+//    @Before
+//	public void setUp() throws Exception {
+//    	collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest();
+//	}
     
     @Ignore("This isn't actually possible to test, because the test is actually thrown by the rule, which is further up the callstack than the test method")    @Test(expected=AssertionError.class)
     public void invocationOnCollaboratorIsIgnored() {