Skip to content

Instantly share code, notes, and snippets.

@hachi-eiji
Last active December 11, 2015 11:48
Show Gist options
  • Save hachi-eiji/4595993 to your computer and use it in GitHub Desktop.
Save hachi-eiji/4595993 to your computer and use it in GitHub Desktop.
EasyMock Test
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="jp.ne.hachi" />
<bean id="easyMockFactoryBean" class="jp.ne.hachi.util.EasyMockFactoryBean">
<constructor-arg index="0" value="jp.ne.hachi.dao.UserDao" />
</bean>
</beans>
package jp.ne.hachi.util;
import org.easymock.EasyMock;
import org.springframework.beans.factory.config.AbstractFactoryBean;
public class EasyMockFactoryBean extends AbstractFactoryBean {
private Class<?> mockType;
public EasyMockFactoryBean(Class<?> type) {
mockType = type;
}
@Override
protected Object createInstance() throws Exception {
return EasyMock.createMock(mockType);
}
@Override
public Class getObjectType() {
return mockType;
}
}
package jp.ne.hachi.service.impl;
import java.util.ArrayList;
import java.util.List;
import jp.ne.hachi.dao.UserDao;
import jp.ne.hachi.dao.bean.UserBean;
import jp.ne.hachi.service.TestService;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config-context.xml" })
public class TestServiceImplTest {
@Autowired
private TestService tester;
@Autowired
private UserDao userDao;
@Test
public void test() throws InstantiationException, IllegalAccessException {
UserBean bean = new UserBean();
List<UserBean> list = new ArrayList<UserBean>();
UserBean e = new UserBean();
e.setUserId(1L);
list.add(e);
EasyMock.expect(userDao.findAll(bean)).andReturn(list);
EasyMock.replay(userDao);
tester.execute(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment