Commits

kubek2k  committed 40e1083

fixing @Primary overriding #refs #18

  • Participants
  • Parent commits b969d35

Comments (0)

Files changed (5)

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

 
 class MockitoBeansDefiner {
     public AbstractBeanDefinition createMockFactoryBeanDefinition(Class<?> mockClass, Class[] extraInterfaces, String mockName, Answers defaultAnswer) {
-        return BeanDefinitionBuilder.genericBeanDefinition(MockFactoryBean.class.getCanonicalName())
+        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(MockFactoryBean.class.getCanonicalName())
                 .addConstructorArgValue(mockClass)
                 .addConstructorArgValue(extraInterfaces)
                 .addConstructorArgValue(mockName)
                 .addConstructorArgValue(defaultAnswer)
                 .getBeanDefinition();
+        beanDefinition.setPrimary(true);
+        return beanDefinition;
     }
 }

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

 locations = "classpath:/mockContext.xml")
 public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractTestNGSpringContextTests {
     
-    @ReplaceWithMock()
+    @ReplaceWithMock
     @Autowired
     private InnerBean innerBean;
     

File springockito-annotations/src/test/java/org/kubek2k/springockito/annotations/primary/PrimaryMockDefinitionTest.java

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

File springockito-annotations/src/test/resources/mockPrimaryContext.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" primary="true" />
+</beans>

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

 package org.kubek2k.mockito.spring;
 
+import org.springframework.beans.factory.parsing.BeanComponentDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
 import org.w3c.dom.Element;
     protected void doParse(Element element, BeanDefinitionBuilder bean) {
         bean.addConstructorArgValue(element.getAttribute("class"));
     }
+
+    @Override
+    protected void postProcessComponentDefinition(BeanComponentDefinition componentDefinition) {
+        super.postProcessComponentDefinition(componentDefinition);
+        componentDefinition.getBeanDefinition().setPrimary(true);
+    }
 }