kubek2k avatar kubek2k committed 608a7ba Merge

Springockito annotations added to the default branch

Comments (0)

Files changed (47)

 syntax regexp
 target/.*
+.*/.project
+.*/.classpath
+.*/.settings
 0000000000000000000000000000000000000000 springockito-1.0.0
 0000000000000000000000000000000000000000 springockito-1.0.0
 e7ff9b4d41e25173c6e3d8765a41fff16546ed94 springockito-1.0.0
+0bcf2e557c5bbd0162a15392a899ab821537e493 remove
+e7ff9b4d41e25173c6e3d8765a41fff16546ed94 springockito-1.0.0
+0000000000000000000000000000000000000000 springockito-1.0.0
+0bcf2e557c5bbd0162a15392a899ab821537e493 remove
+0000000000000000000000000000000000000000 remove
+0000000000000000000000000000000000000000 springockito-1.0.0
+d3564f93384001a57908f9c61b2a311de02de997 springockito-1.0.0
+d3564f93384001a57908f9c61b2a311de02de997 springockito-1.0.0
+0000000000000000000000000000000000000000 springockito-1.0.0
+0000000000000000000000000000000000000000 springockito-1.0.0
+2d54aadfc9c349ec019f29460d131d764f4c6d6e springockito-1.0.0
+1522f6bb34128a065063fd2f704c587f65641b21 springockito-1.0.1-SNAPSHOT
+13ad762889897df1486951b853bd8e0fdbf5ab2e springockito-parent-1.0.0-SNAPSHOT
+31639b7f31d624077a81715ef4e8effaeb98fbeb springockito-1.0.2-SNAPSHOT
+6875ba566f9fccfe6c4c83431f58c66e7caadec4 springockito-annotations-1.0.0-SNAPSHOT
 <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.mockito.spring</groupId>
-    <artifactId>springockito</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.kubek2k</groupId>
+  <artifactId>springockito-parent</artifactId>
+  <version>1.0.1-SNAPSHOT</version>
+  <url>https://bitbucket.org/kubek2k/springockito</url>
+  <name>Springockito parent</name>
+  <packaging>pom</packaging>
+
+  <parent>
+    <groupId>org.sonatype.oss</groupId>
+    <artifactId>oss-parent</artifactId>
+    <version>7</version>
+  </parent>
+
+  <licenses>
+    <license>
+      <name>MIT</name>
+      <url>http://www.opensource.org/licenses/mit-license.php</url>
+      <comments>Copyright (c) 2011 Jakub Janczak</comments>
+    </license>
+  </licenses>
+
+  <developers>
+    <developer>
+      <id>kubek2k</id>
+      <email>kubek2k@gmail.com</email>
+      <name>Jakub Janczak</name>
+    </developer>     
+  </developers>
+
+  <scm>
     <url>https://bitbucket.org/kubek2k/springockito</url>
-    <description>Simple tool to create mocks in Spring environment</description>
+    <connection>https://bitbucket.org/kubek2k/springockito/</connection>
+    <developerConnection>scm:hg:https://bitbucket.org/kubek2k/springockito/</developerConnection>
+  </scm>
 
-    <parent>
-      <groupId>org.sonatype.oss</groupId>
-      <artifactId>oss-parent</artifactId>
-      <version>7</version>
-    </parent>
-
-    <properties>
-        <spring.version>3.0.5.RELEASE</spring.version>
-    </properties>
-
-    <licenses>
-		<license>
-			<name>MIT</name>
-			<url>http://www.opensource.org/licenses/mit-license.php</url>
-			<comments>Copyright (c) 2011 Jakub Janczak</comments>
-		</license>
-	</licenses>
-
-    <developers>
-        <developer>
-            <id>kubek2k</id>
-            <email>kubek2k@gmail.com</email>
-            <name>Jakub Janczak</name>
-        </developer>     
-    </developers>
-
-    <scm>
-        <url>https://bitbucket.org/kubek2k/springockito</url>
-        <connection>https://bitbucket.org/kubek2k/springockito/src</connection>
-        <developerConnection>scm:hg:https://bitbucket.org/kubek2k/springockito</developerConnection>
-    </scm>
-
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-compiler-plugin</artifactId>
-                <version>2.3.2</version>
-                <configuration>
-                    <source>1.5</source>
-                    <target>1.5</target>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
-
-    <dependencies>
-        <dependency>
-            <groupId>org.mockito</groupId>
-            <artifactId>mockito-all</artifactId>
-            <version>1.8.5</version>
-        </dependency>
-
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-core</artifactId>
-            <version>${spring.version}</version>
-						<scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-expression</artifactId>
-						<version>${spring.version}</version>
-						<scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-beans</artifactId>
-						<version>${spring.version}</version>
-						<scope>provided</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-context</artifactId>
-						<version>${spring.version}</version>
-						<scope>provided</scope>
-        </dependency>
-
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-test</artifactId>
-            <version>${spring.version}</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.testng</groupId>
-            <artifactId>testng</artifactId>
-            <version>5.14.2</version>
-            <scope>test</scope>
-        </dependency>
-    </dependencies>
+  <build>
+  <pluginManagement>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</version>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <artifactId>maven-release-plugin</artifactId>
+        <version>2.2</version>
+        <configuration>
+          <mavenExecutorId>forked-path</mavenExecutorId>
+          <useReleaseProfile>false</useReleaseProfile>
+          <arguments>-Psonatype-oss-release</arguments>
+        </configuration>
+      </plugin>
+    </plugins>
+  </pluginManagement>
+</build>
 </project>

springockito-annotations/pom.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<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-annotations</artifactId>
+  <version>1.0.1-SNAPSHOT</version>
+  <name>Springockito annotations</name>
+
+  <properties>
+    <spring.version>3.0.5.RELEASE</spring.version>
+  </properties>
+
+  <parent>
+    <groupId>org.kubek2k</groupId>
+    <artifactId>springockito-parent</artifactId>
+    <version>1.0.0</version>
+  </parent>
+  
+  <scm>
+    <url>https://bitbucket.org/kubek2k/springockito</url>
+    <connection>https://bitbucket.org/kubek2k/springockito</connection>
+    <developerConnection>scm:hg:https://bitbucket.org/kubek2k/springockito</developerConnection>
+  </scm>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-context</artifactId>
+      <version>${spring.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-test</artifactId>
+      <version>${spring.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <version>1.8.5</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.7</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/DesiredMockitoBeansFinder.java

+package org.kubek2k.springockito.annotations;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+class DesiredMockitoBeansFinder {
+    public Map<String, Class<?>> findMockedBeans(Class<?> clazz) {
+        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), ReplaceWithMock.class);
+    }
+
+    public Set<String> findSpiedBeans(Class<?> clazz) {
+        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), WrapWithSpy.class).keySet();
+    }
+
+    private Map<String, Class<?>> findAnnotatedFieldsTypes(Field[] fieldsToScan, Class<? extends Annotation> annotationClass) {
+        Map<String, Class<?>> mockedBeans = new HashMap<String, Class<?>>();
+        for (Field field : fieldsToScan) {
+            if (field.getAnnotation(annotationClass) != null) {
+                mockedBeans.put(field.getName(), field.getType());
+            }
+        }
+        return mockedBeans;
+    }
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/MockitoBeansDefiner.java

+package org.kubek2k.springockito.annotations;
+
+import org.kubek2k.springockito.annotations.factory.MockFactoryBean;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+
+class MockitoBeansDefiner {
+    public AbstractBeanDefinition createMockFactoryBeanDefinition(Class<?> mockClass) {
+        return BeanDefinitionBuilder.genericBeanDefinition(MockFactoryBean.class.getCanonicalName())
+                .addConstructorArgValue(mockClass)
+                .getBeanDefinition();
+    }
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/MockitoSpiesDefiner.java

+package org.kubek2k.springockito.annotations;
+
+import org.kubek2k.springockito.annotations.factory.SpyFactoryBean;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+
+public class MockitoSpiesDefiner {
+
+    AbstractBeanDefinition createSpyDefinition(String wrappedBeanName) {
+        return BeanDefinitionBuilder
+                .rootBeanDefinition(SpyFactoryBean.class.getCanonicalName())
+                .addConstructorArgReference(wrappedBeanName)
+                .getBeanDefinition();
+    }
+
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/ReplaceWithMock.java

+package org.kubek2k.springockito.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface ReplaceWithMock {
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/SpringockitoContextLoader.java

+package org.kubek2k.springockito.annotations;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.test.context.support.GenericXmlContextLoader;
+
+public class SpringockitoContextLoader extends GenericXmlContextLoader {
+
+    private Map<String, Class<?>> mockedBeans = new HashMap<String, Class<?>>();
+    private Set<String> spiedBeans;
+
+    private DesiredMockitoBeansFinder mockedBeansFinder = new DesiredMockitoBeansFinder();
+
+    private MockitoBeansDefiner mockitoBeansDefiner = new MockitoBeansDefiner();
+    private MockitoSpiesDefiner mockitoSpiesDefiner = new MockitoSpiesDefiner();
+
+    @Override
+    protected void customizeContext(GenericApplicationContext context) {
+        super.customizeContext(context);
+        registerMocks(context, mockedBeans);
+        registerSpies(context, spiedBeans);
+    }
+
+    private void registerMocks(GenericApplicationContext context, Map<String, Class<?>> mockedBeans) {
+        for (Map.Entry<String, Class<?>> beanEntry : this.mockedBeans.entrySet()) {
+            context.registerBeanDefinition(beanEntry.getKey(),
+                    mockitoBeansDefiner.createMockFactoryBeanDefinition(beanEntry.getValue()));
+        }
+    }
+
+    private void registerSpies(GenericApplicationContext context, Set<String> spiedBeanNames) {
+        for (String beanName : spiedBeanNames) {
+            BeanDefinition beanDefinition = context.getBeanDefinition(beanName);
+            String wrappedBeanName = beanName + "$$WRAPPED_WITH_SPY";
+            context.registerBeanDefinition(wrappedBeanName, beanDefinition);
+            context.registerBeanDefinition(beanName, mockitoSpiesDefiner.createSpyDefinition(wrappedBeanName));
+        }
+    }
+
+    @Override
+    protected String[] generateDefaultLocations(Class<?> clazz) {
+        this.mockedBeans = mockedBeansFinder.findMockedBeans(clazz);
+        this.spiedBeans = mockedBeansFinder.findSpiedBeans(clazz);
+
+        return super.generateDefaultLocations(clazz);
+
+    }
+
+    @Override
+    protected String[] modifyLocations(Class<?> clazz, String... locations) {
+        this.mockedBeans = mockedBeansFinder.findMockedBeans(clazz);
+        this.spiedBeans = mockedBeansFinder.findSpiedBeans(clazz);
+
+        return super.modifyLocations(clazz, locations);
+    }
+
+    void setMockedBeansFinder(DesiredMockitoBeansFinder mockedBeansFinder) {
+        this.mockedBeansFinder = mockedBeansFinder;
+    }
+
+    void setMockitoBeansDefiner(MockitoBeansDefiner mockitoBeansDefiner) {
+        this.mockitoBeansDefiner = mockitoBeansDefiner;
+    }
+    
+    void setMockitoSpiesDefiner(MockitoSpiesDefiner mockitoSpiesDefiner) {
+        this.mockitoSpiesDefiner = mockitoSpiesDefiner;
+    }
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/WrapWithSpy.java

+package org.kubek2k.springockito.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface WrapWithSpy {
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/factory/MockFactoryBean.java

+package org.kubek2k.springockito.annotations.factory;
+
+import org.mockito.Mockito;
+import org.springframework.beans.factory.FactoryBean;
+
+
+public class MockFactoryBean<T> implements FactoryBean<T> {
+
+    private Class<T> mockClass;
+    private T instance;
+
+    public MockFactoryBean(Class<T> mockClass) {
+        this.mockClass = mockClass;
+    }
+
+    public Class<? extends T> getObjectType() {
+        return mockClass;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public T getObject() throws Exception {
+        if (instance == null) {
+            instance = Mockito.mock(mockClass);
+        }
+        return instance;
+    }
+
+    protected T createInstance(Class<T> mockClass) {
+        return Mockito.mock(mockClass);
+    }
+
+}

springockito-annotations/src/main/java/org/kubek2k/springockito/annotations/factory/SpyFactoryBean.java

+package org.kubek2k.springockito.annotations.factory;
+
+import org.mockito.Mockito;
+import org.springframework.beans.factory.FactoryBean;
+
+public class SpyFactoryBean<T> implements FactoryBean<T> {
+
+    private T wrappedInstance;
+
+    private T spyInstance;
+    
+    public SpyFactoryBean(T wrappedInstance) {
+        this.wrappedInstance = wrappedInstance;
+    }
+    
+    public T getObject() throws Exception {
+        if (spyInstance == null) {
+            spyInstance = Mockito.spy(wrappedInstance);
+        }
+        
+        return spyInstance;
+    }
+
+    @SuppressWarnings("unchecked")
+    public Class<? extends T> getObjectType() {
+        return (Class<? extends T>) wrappedInstance.getClass();
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/InnerBean.java

+package org.kubek2k.springockito.annotations;
+
+public class InnerBean {
+
+    public static final int VALUE_RETURNED_BY_INNER = 300;
+
+    public int doSomething() {
+        return VALUE_RETURNED_BY_INNER;
+    }
+
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/OuterBean.java

+package org.kubek2k.springockito.annotations;
+
+public class OuterBean {
+    private InnerBean innerBean;
+
+    public void setInnerBean(InnerBean innerBean) {
+        this.innerBean = innerBean;
+    }
+    
+    public int doSomething() {
+        return innerBean.doSomething();
+    }
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/OuterBeanWithAutowired.java

+package org.kubek2k.springockito.annotations;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class OuterBeanWithAutowired {
+
+    @Autowired
+    private InnerBean innerBean;
+
+    public void setInnerBean(InnerBean innerBean) {
+        this.innerBean = innerBean;
+    }
+
+    public int doSomething() {
+        return innerBean.doSomething();
+    }
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/SpringockitoAnnotationsComponentScanMocksIntegrationTest.java

+package org.kubek2k.springockito.annotations;
+
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:/componentScanMockContext.xml")
+public class SpringockitoAnnotationsComponentScanMocksIntegrationTest extends AbstractJUnit4SpringContextTests {
+
+    @ReplaceWithMock
+    @Autowired
+    private InnerBean innerBean;
+
+    @Autowired
+    private OuterBeanWithAutowired outerBean;
+
+    @Test
+    @DirtiesContext
+    public void shouldUseMockInsteadOfOriginalBean() {
+        outerBean.doSomething();
+
+        verify(innerBean).doSomething();
+    }
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/SpringockitoAnnotationsComponentScanSpiesIntegrationTest.java

+package org.kubek2k.springockito.annotations;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+import static org.mockito.Mockito.verify;
+
+@ContextConfiguration(loader = SpringockitoContextLoader.class,
+locations = "classpath:/componentScanMockContext.xml")
+public class SpringockitoAnnotationsComponentScanSpiesIntegrationTest extends AbstractJUnit4SpringContextTests {
+    
+    @WrapWithSpy
+    @Autowired
+    private InnerBean innerBean;
+    
+    @Autowired
+    private OuterBeanWithAutowired outerBean;
+    
+    @Test
+    @DirtiesContext
+    public void shouldUseMockInsteadOfOriginalBean() {
+        int result = outerBean.doSomething();
+        
+        Assert.assertEquals(InnerBean.VALUE_RETURNED_BY_INNER, result);
+        verify(innerBean).doSomething();
+    }
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/SpringockitoAnnotationsMocksIntegrationTest.java

+package org.kubek2k.springockito.annotations;
+
+import static org.mockito.Mockito.verify;
+
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration(loader = SpringockitoContextLoader.class,
+locations = "classpath:/mockContext.xml")
+public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {
+    
+    @ReplaceWithMock
+    @Autowired
+    private InnerBean innerBean;
+    
+    @Autowired
+    private OuterBean outerBean;
+    
+    @Test
+    @DirtiesContext
+    public void shouldUseMockInsteadOfOriginalBean() {
+        outerBean.doSomething();
+        
+        verify(innerBean).doSomething();
+    }
+    
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/SpringockitoAnnotationsSpiesIntegrationTest.java

+package org.kubek2k.springockito.annotations;
+
+import static org.mockito.Mockito.verify;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration(loader = SpringockitoContextLoader.class,
+locations = "classpath:/mockContext.xml")
+public class SpringockitoAnnotationsSpiesIntegrationTest extends AbstractJUnit4SpringContextTests {
+
+    @WrapWithSpy
+    @Autowired
+    private InnerBean innerBean;
+    
+    @Autowired
+    private OuterBean outerBean;
+    
+    @Test
+    @DirtiesContext
+    public void shouldUseSpyInsteadOfOriginalBean() {
+        int result = outerBean.doSomething();
+        
+        // the actual returned value is real and we can verify interaction
+        Assert.assertEquals(InnerBean.VALUE_RETURNED_BY_INNER, result);
+        verify(innerBean).doSomething();
+    }
+    
+}

springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/SpringockitoContextLoaderTest.java

+package org.kubek2k.springockito.annotations;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+import org.junit.Test;
+import org.mockito.Spy;
+import org.springframework.context.ApplicationContext;
+
+public class SpringockitoContextLoaderTest {
+    @Test
+    public void shouldLoadMockBean() throws Exception {
+        // given
+        SpringockitoContextLoader loader = new SpringockitoContextLoader();
+
+        // when
+        loader.processLocations(SomeTestClass.class);
+        ApplicationContext context = loader.loadContext("classpath:/mockContext.xml");
+
+        // then
+        OuterBean outerBean = (OuterBean) context.getBean("outerBean1");
+
+        // verification that it's a mock
+        verifyNoMoreInteractions(outerBean);
+
+    }
+    
+    @Test
+    public void shouldLoadSpyBean() throws Exception {
+        // given
+        SpringockitoContextLoader loader = new SpringockitoContextLoader();
+
+        // when
+        loader.processLocations(SomeTestClass.class);
+        ApplicationContext context = loader.loadContext("classpath:/mockContext.xml");
+
+        // then
+        OuterBean outer =  (OuterBean) context.getBean("outerBean");
+        outer.doSomething();
+        
+        // verification that it's a spy
+        verify(outer).doSomething();
+    }
+
+    public static class SomeTestClass {
+        @ReplaceWithMock
+        private OuterBean outerBean1;
+        
+        @WrapWithSpy
+        private OuterBean outerBean;
+    }
+
+
+    public static interface X {
+        public int hello();
+    }
+
+}

springockito-annotations/src/test/resources/componentScanMockContext.xml

+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:context="http://www.springframework.org/schema/context"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+    <context:component-scan base-package="org.kubek2k.springockito.annotations"/>
+
+    <bean id="innerBean" class="org.kubek2k.springockito.annotations.InnerBean" />
+</beans>

springockito-annotations/src/test/resources/mockContext.xml

+<beans xmlns="http://www.springframework.org/schema/beans"
+    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="outerBean" class="org.kubek2k.springockito.annotations.OuterBean">
+        <property name="innerBean" ref="innerBean" />
+    </bean>
+    
+    <bean id="innerBean" class="org.kubek2k.springockito.annotations.InnerBean" />
+</beans>

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">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.kubek2k</groupId>
+    <artifactId>springockito</artifactId>
+    <version>1.0.3-SNAPSHOT</version>
+    <url>https://bitbucket.org/kubek2k/springockito</url>
+    <description>Simple tool to create mocks in Spring environment</description>
+    <name>Springockito</name>
+
+    <properties>
+      <spring.version>3.0.5.RELEASE</spring.version>
+    </properties>
+
+    <parent>
+      <groupId>org.kubek2k</groupId>
+      <artifactId>springockito-parent</artifactId>
+      <version>1.0.0</version>
+    </parent>
+    
+    <scm>
+      <url>https://bitbucket.org/kubek2k/springockito</url>
+      <connection>https://bitbucket.org/kubek2k/springockito</connection>
+      <developerConnection>scm:hg:https://bitbucket.org/kubek2k/springockito</developerConnection>
+    </scm>
+
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.5</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
+            <version>${spring.version}</version>
+						<scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-expression</artifactId>
+						<version>${spring.version}</version>
+						<scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-beans</artifactId>
+						<version>${spring.version}</version>
+						<scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+						<version>${spring.version}</version>
+						<scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>${spring.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <version>5.14.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

springockito/src/main/java/org/kubek2k/mockito/spring/MockitoMockBeanDefinitionParser.java

+package org.kubek2k.mockito.spring;
+
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
+import org.w3c.dom.Element;
+
+public class MockitoMockBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
+    
+    @Override
+    protected String getBeanClassName(Element element) {
+        return "org.mockito.Mockito";
+    }
+    
+    @Override
+    protected void doParse(Element element, BeanDefinitionBuilder bean) {
+        bean.setFactoryMethod("mock").addConstructorArgValue(element.getAttribute("class"));
+    }
+}

springockito/src/main/java/org/kubek2k/mockito/spring/MockitoNamespaceHandler.java

+package org.kubek2k.mockito.spring;
+
+import org.springframework.beans.factory.xml.NamespaceHandler;
+import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
+
+public class MockitoNamespaceHandler extends NamespaceHandlerSupport implements NamespaceHandler {
+
+    public void init() {
+        registerBeanDefinitionParser("mock", new MockitoMockBeanDefinitionParser());
+        registerBeanDefinitionParser("spy", new MockitoSpyBeanDefinitionParser());
+    }
+
+}

springockito/src/main/java/org/kubek2k/mockito/spring/MockitoSpyBeanDefinitionParser.java

+package org.kubek2k.mockito.spring;
+
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
+import org.w3c.dom.Element;
+
+public class MockitoSpyBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
+
+    @Override
+    protected String getBeanClassName(Element element) {
+        return "org.kubek2k.mockito.spring.MockitoSpyBeanPostProcessor";
+    }
+
+    @Override
+    protected void doParse(Element element, BeanDefinitionBuilder beanBuilder) {
+        beanBuilder.addPropertyValue("beanName", element.getAttribute("beanName"));
+    }
+    
+    @Override
+    protected boolean shouldGenerateIdAsFallback() {
+        return true;
+    }
+}

springockito/src/main/java/org/kubek2k/mockito/spring/MockitoSpyBeanPostProcessor.java

+package org.kubek2k.mockito.spring;
+
+import org.mockito.Mockito;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
+public class MockitoSpyBeanPostProcessor implements BeanPostProcessor {
+
+    private String beanName;
+
+    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+        return bean;
+    }
+
+    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+        if (this.beanName.equals(beanName)) {
+            return Mockito.spy(bean);
+        } else {
+            return bean;
+        }
+    }
+    public void setBeanName(String matchingName) {
+        this.beanName = matchingName;
+    }
+
+}

springockito/src/main/resources/META-INF/spring.handlers

+http\://www.mockito.org/spring/mockito=org.kubek2k.mockito.spring.MockitoNamespaceHandler

springockito/src/main/resources/META-INF/spring.schemas

+http\://www.mockito.org/spring/mockito.xsd=spring/mockito.xsd

springockito/src/main/resources/spring/mockito.xsd

+<?xml version="1.0" encoding="UTF-8"?>
+<xsd:schema xmlns="http://www.mockito.org/spring/mockito"
+    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    xmlns:beans="http://www.springframework.org/schema/beans"
+    targetNamespace="http://www.mockito.org/spring/mockito"
+    elementFormDefault="qualified"
+    attributeFormDefault="unqualified">
+
+   <xsd:import namespace="http://www.springframework.org/schema/beans"/>
+
+   <xsd:element name="mock">
+      <xsd:complexType>
+         <xsd:complexContent>
+            <xsd:extension base="beans:identifiedType">
+               <xsd:attribute name="class" type="xsd:string" use="required"/>
+            </xsd:extension>
+         </xsd:complexContent>
+      </xsd:complexType>
+   </xsd:element>
+   
+   <xsd:element name="spy">
+      <xsd:complexType>
+         <xsd:attribute name="beanName" type="xsd:string" use="required"/>
+      </xsd:complexType>
+   </xsd:element>
+
+</xsd:schema>

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 java.util.Date;
+
+import org.mockito.cglib.proxy.Factory;
+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/mockitoContext.xml"})
+public class MockitoMockHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
+    
+    @Autowired
+    private Date someDate;
+    
+    @Test
+    public void shouldLoadMockitoMock() {
+        assertNotNull(someDate);
+        assertTrue(someDate instanceof Factory);
+    }
+}

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.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" })
+public class MockitoSpyHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
+
+    @Autowired
+    private Date dateToBeSpied;
+    
+    @Test
+    public void shouldLoadMockitoSpy() {
+        dateToBeSpied.compareTo(new Date());
+        verify(dateToBeSpied).compareTo(isA(Date.class));
+    }
+}

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
+    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
+    
+    <mockito:mock class="java.util.Date" id="someDate" />
+
+</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
+    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
+    
+    <mockito:spy beanName="dateToBeSpied" />
+    
+</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
+    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
+
+    <bean id="dateToBeSpied" class="java.util.Date" />
+    
+</beans>

src/main/java/org/kubek2k/mockito/spring/MockitoMockBeanDefinitionParser.java

-package org.kubek2k.mockito.spring;
-
-import org.springframework.beans.factory.support.BeanDefinitionBuilder;
-import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
-import org.w3c.dom.Element;
-
-public class MockitoMockBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
-    
-    @Override
-    protected String getBeanClassName(Element element) {
-        return "org.mockito.Mockito";
-    }
-    
-    @Override
-    protected void doParse(Element element, BeanDefinitionBuilder bean) {
-        bean.setFactoryMethod("mock").addConstructorArgValue(element.getAttribute("class"));
-    }
-}

src/main/java/org/kubek2k/mockito/spring/MockitoNamespaceHandler.java

-package org.kubek2k.mockito.spring;
-
-import org.springframework.beans.factory.xml.NamespaceHandler;
-import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
-
-public class MockitoNamespaceHandler extends NamespaceHandlerSupport implements NamespaceHandler {
-
-    public void init() {
-        registerBeanDefinitionParser("mock", new MockitoMockBeanDefinitionParser());
-        registerBeanDefinitionParser("spy", new MockitoSpyBeanDefinitionParser());
-    }
-
-}

src/main/java/org/kubek2k/mockito/spring/MockitoSpyBeanDefinitionParser.java

-package org.kubek2k.mockito.spring;
-
-import org.springframework.beans.factory.support.BeanDefinitionBuilder;
-import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
-import org.w3c.dom.Element;
-
-public class MockitoSpyBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
-
-    @Override
-    protected String getBeanClassName(Element element) {
-        return "org.kubek2k.mockito.spring.MockitoSpyBeanPostProcessor";
-    }
-
-    @Override
-    protected void doParse(Element element, BeanDefinitionBuilder beanBuilder) {
-        beanBuilder.addPropertyValue("beanName", element.getAttribute("beanName"));
-    }
-    
-    @Override
-    protected boolean shouldGenerateIdAsFallback() {
-        return true;
-    }
-}

src/main/java/org/kubek2k/mockito/spring/MockitoSpyBeanPostProcessor.java

-package org.kubek2k.mockito.spring;
-
-import org.mockito.Mockito;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
-
-public class MockitoSpyBeanPostProcessor implements BeanPostProcessor {
-
-    private String beanName;
-
-    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
-        return bean;
-    }
-
-    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-        if (this.beanName.equals(beanName)) {
-            return Mockito.spy(bean);
-        } else {
-            return bean;
-        }
-    }
-    public void setBeanName(String matchingName) {
-        this.beanName = matchingName;
-    }
-
-}

src/main/resources/META-INF/spring.handlers

-http\://www.mockito.org/spring/mockito=org.kubek2k.mockito.spring.MockitoNamespaceHandler

src/main/resources/META-INF/spring.schemas

-http\://www.mockito.org/spring/mockito.xsd=spring/mockito.xsd

src/main/resources/spring/mockito.xsd

-<?xml version="1.0" encoding="UTF-8"?>
-<xsd:schema xmlns="http://www.mockito.org/spring/mockito"
-    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-    xmlns:beans="http://www.springframework.org/schema/beans"
-    targetNamespace="http://www.mockito.org/spring/mockito"
-    elementFormDefault="qualified"
-    attributeFormDefault="unqualified">
-
-   <xsd:import namespace="http://www.springframework.org/schema/beans"/>
-
-   <xsd:element name="mock">
-      <xsd:complexType>
-         <xsd:complexContent>
-            <xsd:extension base="beans:identifiedType">
-               <xsd:attribute name="class" type="xsd:string" use="required"/>
-            </xsd:extension>
-         </xsd:complexContent>
-      </xsd:complexType>
-   </xsd:element>
-   
-   <xsd:element name="spy">
-      <xsd:complexType>
-         <xsd:attribute name="beanName" type="xsd:string" use="required"/>
-      </xsd:complexType>
-   </xsd:element>
-
-</xsd:schema>

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 java.util.Date;
-
-import org.mockito.cglib.proxy.Factory;
-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/mockitoContext.xml"})
-public class MockitoMockHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
-    
-    @Autowired
-    private Date someDate;
-    
-    @Test
-    public void shouldLoadMockitoMock() {
-        assertNotNull(someDate);
-        assertTrue(someDate instanceof Factory);
-    }
-}

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.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" })
-public class MockitoSpyHandlerIntegrationTest extends AbstractTestNGSpringContextTests {
-
-    @Autowired
-    private Date dateToBeSpied;
-    
-    @Test
-    public void shouldLoadMockitoSpy() {
-        dateToBeSpied.compareTo(new Date());
-        verify(dateToBeSpied).compareTo(isA(Date.class));
-    }
-}

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
-    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
-    
-    <mockito:mock class="java.util.Date" id="someDate" />
-
-</beans>

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
-    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
-    
-    <mockito:spy beanName="dateToBeSpied" />
-    
-</beans>

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
-    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
-
-    <bean id="dateToBeSpied" class="java.util.Date" />
-    
-</beans>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.