Spring + EclipselinkをJunitでテスト
昨日の続き。
ユニットテストのない現場にはもううんざりなので、JunitでSpring + Eclipselinkのテストのやり方を調べる。
たったそれだけのこと、と思うだろうか。
でも今のうちの現場にはたったそれだけのことができない。
あと僕もそれだけのことなのに、調べるのに半日も費やしてしまった。
persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="Test" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.yoshida.test.entity.Employee</class> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="eclipselink.cache.shared.default" value="false"/> </properties> </persistence-unit> </persistence>
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.yoshida.test.service,com.yoshida.test.dao" /> <context:load-time-weaver aspectj-weaving="on" /> <tx:annotation-driven mode="aspectj" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.203.128:3306/test" /> <property name="username" value="heppokoact" /> <property name="password" value="admin" /> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" /> <property name="generateDdl" value="false" /> <property name="showSql" value="true" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="Test" /> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" /> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans>
テスト対象クラス
@Service @Transactional @Scope(BeanDefinition.SCOPE_PROTOTYPE) public class TestServiceImpl implements TestService { public int test() { return 100; } } @Repository @Scope(BeanDefinition.SCOPE_PROTOTYPE) public class TestDaoImpl implements TestDao { @PersistenceContext private EntityManager em; @Override public Employee getEmployeeByPk(int id) { return em.find(Employee.class, id); } }
テストクラス
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="/applicationContext.xml") @TransactionConfiguration @Transactional public class TestServiceImplTest { @Autowired private TestService service; @Autowired private TestDao dao; @Test public void Springとの連携をテストする() { assertEquals(100, service.test()); } @Test public void データベースからデータを取得してみる() { Employee employee = dao.getEmployeeByPk(1); assertEquals("淀橋亀羅", employee.getName()); assertEquals(250000, employee.getSalary()); } }
JUunitのVM起動引数(eclipse使用。パスは自分の環境に合わせる)
-javaagent:${project_loc:Test}/WebContent/WEB-INF/lib/spring-agent-2.5.6.jar
どうにかならないの?
- JUunitのVM起動引数は起動構成ごとに指定しなければならない。どこかで一括して指定できないだろうか?
- EntitiyMnanagerFactoryの初期化があまりにも重い。