Commits

pbatko committed 7b2fc53

Change MockFactoryBean to use Mockito.mock() method to create mocks as mockito needs a mock to be instance of org.mockito.internal.creation.MethodInterceptorFilter whereas old solution ensured only implementing org.mockito.cglib.proxy.MethodInterceptor interface.

  • Participants
  • Parent commits 2d0576a

Comments (0)

Files changed (9)

springockito/pom.xml

-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.kubek2k</groupId>
     <artifactId>springockito</artifactId>
             <artifactId>spring-context</artifactId>
         </dependency>
 
+        <!--test-->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.easytesting</groupId>
+            <artifactId>fest-assert</artifactId>
+            <version>1.4</version>
             <scope>test</scope>
         </dependency>
     </dependencies>

springockito/src/main/java/org/kubek2k/mockito/spring/factory/MockFactoryBean.java

 package org.kubek2k.mockito.spring.factory;
 
 import org.mockito.Mockito;
-import org.mockito.internal.creation.jmock.ClassImposterizer;
 import org.springframework.beans.factory.FactoryBean;
 
 
 
     private Class<T> mockClass;
     private T instance;
-    
+
     public MockFactoryBean(Class<T> mockClass) {
         this.mockClass = mockClass;
     }
 
     public T getObject() throws Exception {
         if (instance == null) {
-            instance = ClassImposterizer.INSTANCE.imposterise(new ThreadLocalMockMethodInterceptor<T>(mockClass), mockClass, new Class[0]);
+            instance = Mockito.mock(mockClass);
         }
         return instance;
     }
 
-    protected T createInstance(Class<T> mockClass) {
-        return Mockito.mock(mockClass);
-    }
-
 }

springockito/src/test/java/org/kubek2k/mockito/spring/MockitoMockHandlerIntegrationTest.java

 package org.kubek2k.mockito.spring;
 
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertTrue;
-
+import org.kubek2k.mockito.spring.testbeans.BeanToBeSpiedOrMockedAndStubbed;
 import org.mockito.cglib.proxy.Factory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
 import org.testng.annotations.Test;
 
+import javax.annotation.Resource;
 
-@ContextConfiguration(locations={"classpath*:/spring/mockitoContext.xml"})
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Matchers.anyString;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+@ContextConfiguration(locations = {"classpath*:/spring/mockitoContext.xml"})
 public class MockitoMockHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
 
     @Autowired
     private SomeFancyClass someFancyClass;
-    
+
     @Autowired
     @Qualifier("someFancyClass")
     private SomeFancyClass someFancyClass2;
-    
+
+    @Resource
+    private BeanToBeSpiedOrMockedAndStubbed beanToBeMockedAndStubbed;
+
     @Test
     public void shouldLoadMockitoMock() {
         assertNotNull(someFancyClass);
         assertTrue(someFancyClass instanceof Factory);
-        
+
         assertNotNull(someFancyClass2);
         assertTrue(someFancyClass2 instanceof Factory);
 
     }
+
+    @Test
+    public void shouldAllowForMocksTeBeStubbedUsingMockitoMatchers() {
+        //given
+        String fixedReturnValue = "fixedReturnValue";
+        given(beanToBeMockedAndStubbed.methodWithArgument(anyString()))
+                .willReturn(fixedReturnValue);
+
+        //when
+        String returnedString = beanToBeMockedAndStubbed.methodWithArgument("someString");
+
+        //then
+        assertThat(returnedString)
+                .isEqualTo(fixedReturnValue);
+    }
 }

springockito/src/test/java/org/kubek2k/mockito/spring/MockitoSpyHandlerIntegrationTest.java

 package org.kubek2k.mockito.spring;
 
-import static org.mockito.Matchers.isA;
-import static org.mockito.Mockito.verify;
-
-import java.util.Date;
-
+import org.kubek2k.mockito.spring.testbeans.BeanToBeInjectedWithSpy;
+import org.kubek2k.mockito.spring.testbeans.BeanToBeSpiedOrMockedAndStubbed;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
 import org.testng.annotations.Test;
 
-@ContextConfiguration(locations = { "classpath*:/spring/parentContext.xml", "classpath*:/spring/mockitoSpyContext.xml" })
+import javax.annotation.Resource;
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@ContextConfiguration(locations = {"classpath*:/spring/parentContext.xml", "classpath*:/spring/mockitoSpyContext.xml"})
 public class MockitoSpyHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
 
     @Autowired
     private Date dateToBeSpied;
-    
+
+    @Resource
+    private BeanToBeSpiedOrMockedAndStubbed beanToBeSpiedAndStubbed;
+
+
+    @Resource
+    BeanToBeInjectedWithSpy beanToBeInjectedWithSpy;
+
+
     @Test
     public void shouldLoadMockitoSpy() {
         dateToBeSpied.compareTo(new Date());
         verify(dateToBeSpied).compareTo(isA(Date.class));
     }
+
+    @Test
+    public void shouldAllowForSpiesTeBeStubbedUsingMockitoMatchers() {
+        //given
+        String fixedReturnValue = "fixedReturnValue";
+        given(beanToBeSpiedAndStubbed.methodWithArgument(anyString()))
+                .willReturn(fixedReturnValue);
+
+        //when
+        String returnedString = beanToBeSpiedAndStubbed.methodWithArgument("someString");
+
+        //then
+        assertThat(returnedString)
+                .isEqualTo(fixedReturnValue);
+    }
+
+    @Test
+    public void shouldInjectSpyOfTheBeanInsteadOfOriginalOneEverywhere() {
+        //given
+        int expectedAmountOfSpyMethodInvocations = 2;
+
+        //when
+        beanToBeInjectedWithSpy.callMethodWithoutArgumentsOnInjectedBeanTwice();
+
+        //then
+        verify(beanToBeSpiedAndStubbed, times(expectedAmountOfSpyMethodInvocations))
+                .methodWithoutArgument();
+    }
 }

springockito/src/test/java/org/kubek2k/mockito/spring/testbeans/BeanToBeInjectedWithSpy.java

+package org.kubek2k.mockito.spring.testbeans;
+
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+@Component
+public class BeanToBeInjectedWithSpy {
+
+    private BeanToBeSpiedOrMockedAndStubbed beanToBeSpiedOrMockedAndStubbed;
+
+    public void callMethodWithoutArgumentsOnInjectedBeanTwice() {
+        beanToBeSpiedOrMockedAndStubbed.methodWithoutArgument();
+        beanToBeSpiedOrMockedAndStubbed.methodWithoutArgument();
+    }
+
+    @Resource
+    public void setBeanToBeSpiedOrMockedAndStubbed(BeanToBeSpiedOrMockedAndStubbed beanToBeSpiedOrMockedAndStubbed) {
+        this.beanToBeSpiedOrMockedAndStubbed = beanToBeSpiedOrMockedAndStubbed;
+    }
+}

springockito/src/test/java/org/kubek2k/mockito/spring/testbeans/BeanToBeSpiedOrMockedAndStubbed.java

+package org.kubek2k.mockito.spring.testbeans;
+
+import org.springframework.stereotype.Component;
+
+@Component
+public class BeanToBeSpiedOrMockedAndStubbed {
+
+    public String methodWithArgument(String string) {
+        return string;
+    }
+
+    public void methodWithoutArgument() {
+    }
+
+}

springockito/src/test/resources/spring/mockitoContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xmlns:mockito="http://www.mockito.org/spring/mockito"
-    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:mockito="http://www.mockito.org/spring/mockito"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.mockito.org/spring/mockito classpath:spring/mockito.xsd">
-    
-    <mockito:mock class="org.kubek2k.mockito.spring.SomeFancyClass" id="someFancyClass" />
+
+    <mockito:mock class="org.kubek2k.mockito.spring.SomeFancyClass" id="someFancyClass"/>
+
+    <mockito:mock class="org.kubek2k.mockito.spring.testbeans.BeanToBeSpiedOrMockedAndStubbed"
+                  id="org.kubek2k.mockito.spring.testbeans.BeanToBeSpiedOrMockedAndStubbed"/>
 
 </beans>

springockito/src/test/resources/spring/mockitoSpyContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xmlns:mockito="http://www.mockito.org/spring/mockito"
-    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:mockito="http://www.mockito.org/spring/mockito"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.mockito.org/spring/mockito classpath:spring/mockito.xsd">
-    
-    <mockito:spy beanName="dateToBeSpied" />
-    
+
+    <mockito:spy beanName="dateToBeSpied"/>
+
+    <mockito:spy beanName="beanToBeSpiedAndStubbed"/>
+
 </beans>

springockito/src/test/resources/spring/parentContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xmlns:mockito="http://www.mockito.org/spring/mockito"
-    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
-    <bean id="dateToBeSpied" class="java.util.Date" />
-    
+    <bean id="dateToBeSpied" class="java.util.Date"/>
+
+    <bean id="beanToBeSpiedAndStubbed" class="org.kubek2k.mockito.spring.testbeans.BeanToBeSpiedOrMockedAndStubbed"/>
+
+    <bean id="beanToBeInjectedWithSpy" class="org.kubek2k.mockito.spring.testbeans.BeanToBeInjectedWithSpy"/>
+
 </beans>