Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating Spring FactoryBeans for Mock Objects
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("failing-mockito-config.xml")
public class BeanWiringTest {
@Autowired
SomeClass someClass;
@Autowired
SomeDependency someDependencyMock;
@Test
public void shouldAutowireDependencies() {
assertNotNull(someClass);
assertNotNull(someDependencyMock);
}
}
<beans...>
<context:component-scan base-package="com.jayway.example"/>
<bean id="someDependencyMock" class="com.jayway.springmock.EasyMockFactoryBean">
<constructor-arg name="classToBeMocked" value="com.jayway.example.SomeDependency" />
</bean>
<!-- Alternative bean declaration to declare a "strict" mock object
<bean id="strictDependencyMock" class="com.jayway.springmock.EasyMockFactoryBean">
<constructor-arg name="classToBeMocked" value="com.jayway.example.SomeDependency" />
<constructor-arg name="mockType" value="STRICT" />
</bean>
-->
</beans>
package com.jayway.springmock;
import org.easymock.EasyMock;
import org.springframework.beans.factory.FactoryBean;
/**
* A {@link FactoryBean} for creating mocked beans based on EasyMock so that they
* can be {@code @Autowired} into Spring test configurations.
*
* @author Mattias Severson, Jayway
*
* @see FactoryBean
* @see org.easymock.EasyMock
*/
public class EasyMockFactoryBean<T> implements FactoryBean<T> {
/**
* Enumeration to define the type of mock.
*/
public enum MockType {
/**
* Mock created by {@link EasyMock#createMock(Class)}
*/
DEFAULT,
/**
* Mock created by {@link EasyMock#createNiceMock(Class)}
*/
NICE,
/**
* Mock created by {@link EasyMock#createStrictMock(Class)}
*/
STRICT
}
private Class<T> classToBeMocked;
private MockType mockType = MockType.DEFAULT;
/**
* Creates an EasyMock mock instance of the provided class of type {@link MockType#DEFAULT}.
* @param classToBeMocked The class to be mocked.
*/
public EasyMockFactoryBean(Class<T> classToBeMocked) {
this(classToBeMocked, MockType.DEFAULT);
}
/**
* Creates a mock instance of the provided class of type {@link MockType#DEFAULT}.
* @param classToBeMocked The class to be mocked.
* @param mockType The {@link MockType} to be created.
*/
public EasyMockFactoryBean(Class<T> classToBeMocked, MockType mockType) {
this.classToBeMocked = classToBeMocked;
this.mockType = mockType;
}
@Override
public T getObject() throws Exception {
switch (mockType) {
case DEFAULT:
return EasyMock.createMock(classToBeMocked);
case NICE:
return EasyMock.createNiceMock(classToBeMocked);
case STRICT:
return EasyMock.createStrictMock(classToBeMocked);
default:
throw new IllegalStateException("Unknown MockType: " + mockType);
}
}
@Override
public Class<T> getObjectType() {
return classToBeMocked;
}
@Override
public boolean isSingleton() {
return true;
}
}
<beans...>
<context:component-scan base-package="com.jayway.example"/>
<bean id="someDependencyMock" class="org.easymock.EasyMock" factory-method="createMock">
<constructor-arg value="com.jayway.example.SomeDependency" />
</bean>
</beans>
<beans...>
<context:component-scan base-package="com.jayway.example"/>
<bean id="someDependencyMock" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.jayway.example.SomeDependency" />
</bean>
</beans>
<beans...>
<context:component-scan base-package="com.jayway.example"/>
<bean id="someDependencyMock" class="com.jayway.springmock.MockitoFactoryBean">
<constructor-arg name="classToBeMocked" value="com.jayway.example.SomeDependency" />
</bean>
</beans>
package com.jayway.springmock;
import org.mockito.Mockito;
import org.springframework.beans.factory.FactoryBean;
/**
* A {@link FactoryBean} for creating mocked beans based on Mockito so that they
* can be {@link @Autowired} into Spring test configurations.
*
* @author Mattias Severson, Jayway
*
* @see FactoryBean
* @see org.mockito.Mockito
*/
public class MockitoFactoryBean<T> implements FactoryBean<T> {
private Class<T> classToBeMocked;
/**
* Creates a Mockito mock instance of the provided class.
* @param classToBeMocked The class to be mocked.
*/
public MockitoFactoryBean(Class<T> classToBeMocked) {
this.classToBeMocked = classToBeMocked;
}
@Override
public T getObject() throws Exception {
return Mockito.mock(classToBeMocked);
}
@Override
public Class<?> getObjectType() {
return classToBeMocked;
}
@Override
public boolean isSingleton() {
return true;
}
}
package com.jayway.example;
@Component
public class SomeClass {
@Autowired
private SomeDependency someDependency;
// more code...
}
package com.jayway.example;
public interface SomeDependency {
}
@matsev

This comment has been minimized.

Copy link
Owner Author

matsev commented Nov 30, 2011

The code in this gist shows how Spring FactoryBeans can be used to simplify the creation of mock objects in Spring integration tests when Mockito and EasyMock are used in Spring integration tests. It is explained in a blog post at http://blog.jayway.com/2011/11/30/spring-integration-tests-part-i-creating-mock-objects/ .

@prerna25

This comment has been minimized.

Copy link

prerna25 commented Apr 25, 2015

I am geeting the erro the type FactoryBean is not generic it cannot be parameterized with arguments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.