Skip to content

Instantly share code, notes, and snippets.

@matsev
Created November 27, 2011 14:12
Show Gist options
  • Save matsev/1397610 to your computer and use it in GitHub Desktop.
Save matsev/1397610 to your computer and use it in GitHub Desktop.
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 {
}
@prerna25
Copy link

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