#29 Junit Rule太高级了,换了一个Spring里的初始化Bean
This commit is contained in:
@@ -101,6 +101,11 @@
|
||||
<jdbc:script location="classpath:sql/h2/schema.sql" />
|
||||
</jdbc:initialize-database>
|
||||
|
||||
<bean class="org.springside.modules.test.data.DefaultDataInitializer" lazy-init="false">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="defaultDataFile" value="/data/sample-data.xml" />
|
||||
</bean>
|
||||
|
||||
<!-- 以静态变量保存ApplicationContext -->
|
||||
<bean class="org.springside.modules.test.spring.SpringContextHolder" lazy-init="false" />
|
||||
</beans>
|
||||
|
||||
-6
@@ -5,12 +5,10 @@ import static org.junit.Assert.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.examples.miniservice.data.AccountData;
|
||||
import org.springside.examples.miniservice.data.SampleDataRule;
|
||||
import org.springside.examples.miniservice.entity.Department;
|
||||
import org.springside.examples.miniservice.entity.User;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
@@ -30,10 +28,6 @@ public class AccountDaoTest extends SpringTxTestCase {
|
||||
@Autowired
|
||||
private AccountDao accountDao;
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
@Test
|
||||
public void getDepartmentDetail() {
|
||||
Department department = accountDao.getDepartmentDetail(1L);
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package org.springside.examples.miniservice.data;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
|
||||
@Component
|
||||
public class SampleDataRule implements TestRule {
|
||||
|
||||
public static boolean dataLoaded = false;
|
||||
|
||||
@Autowired
|
||||
public DataSource dataSource;
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
if (!dataLoaded) {
|
||||
try {
|
||||
Fixtures.reloadData(dataSource, "/data/sample-data.xml");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
dataLoaded = true;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
}
|
||||
@@ -116,6 +116,11 @@
|
||||
<jdbc:script location="classpath:sql/h2/schema.sql" />
|
||||
</jdbc:initialize-database>
|
||||
|
||||
<bean class="org.springside.modules.test.data.DefaultDataInitializer" lazy-init="false">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="defaultDataFile" value="/data/sample-data.xml" />
|
||||
</bean>
|
||||
|
||||
<!-- 以静态变量保存ApplicationContext -->
|
||||
<bean class="org.springside.modules.test.spring.SpringContextHolder" lazy-init="false" />
|
||||
</beans>
|
||||
|
||||
-7
@@ -5,13 +5,10 @@ import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.metamodel.EntityType;
|
||||
import javax.persistence.metamodel.Metamodel;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.examples.miniweb.data.SampleDataRule;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
@@ -22,10 +19,6 @@ public class JpaMappingTest extends SpringTxTestCase {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
@Test
|
||||
public void allClassMapping() throws Exception {
|
||||
Metamodel model = em.getEntityManagerFactory().getMetamodel();
|
||||
|
||||
-6
@@ -5,12 +5,10 @@ import static org.junit.Assert.*;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.examples.miniweb.data.AccountData;
|
||||
import org.springside.examples.miniweb.data.SampleDataRule;
|
||||
import org.springside.examples.miniweb.entity.account.Group;
|
||||
import org.springside.examples.miniweb.entity.account.User;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
@@ -32,10 +30,6 @@ public class GroupDaoTest extends SpringTxTestCase {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
/**
|
||||
* 测试删除权限组时删除用户-权限组的中间表.
|
||||
* @throws Exception
|
||||
|
||||
-6
@@ -5,12 +5,10 @@ import static org.junit.Assert.*;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.examples.miniweb.data.AccountData;
|
||||
import org.springside.examples.miniweb.data.SampleDataRule;
|
||||
import org.springside.examples.miniweb.entity.account.User;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
|
||||
@@ -29,10 +27,6 @@ public class UserDaoTest extends SpringTxTestCase {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
@Test
|
||||
//如果你需要真正插入数据库,将Rollback设为false
|
||||
//@Rollback(false)
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package org.springside.examples.miniweb.data;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
|
||||
@Component
|
||||
public class SampleDataRule implements TestRule {
|
||||
|
||||
public static boolean dataLoaded = false;
|
||||
|
||||
@Autowired
|
||||
public DataSource dataSource;
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
if (!dataLoaded) {
|
||||
try {
|
||||
Fixtures.reloadData(dataSource, "/data/sample-data.xml");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
dataLoaded = true;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
}
|
||||
@@ -138,6 +138,11 @@
|
||||
<jdbc:initialize-database data-source="quartzDataSource" ignore-failures="ALL">
|
||||
<jdbc:script location="classpath:sql/h2/quartz.sql" />
|
||||
</jdbc:initialize-database>
|
||||
|
||||
<bean class="org.springside.modules.test.data.DefaultDataInitializer" lazy-init="false">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="defaultDataFile" value="/data/sample-data.xml" />
|
||||
</bean>
|
||||
|
||||
<!-- SpringContextHolder定义 -->
|
||||
<bean class="org.springside.modules.test.spring.SpringContextHolder" lazy-init="false" />
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package org.springside.examples.showcase.data;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
|
||||
@Component
|
||||
public class SampleDataRule implements TestRule {
|
||||
|
||||
public static boolean dataLoaded = false;
|
||||
|
||||
@Autowired
|
||||
public DataSource dataSource;
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
if (!dataLoaded) {
|
||||
try {
|
||||
Fixtures.reloadData(dataSource, "/data/sample-data.xml");
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
dataLoaded = true;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
}
|
||||
-7
@@ -3,13 +3,10 @@ package org.springside.examples.showcase.schedule;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springside.examples.showcase.data.SampleDataRule;
|
||||
import org.springside.modules.log.MockLog4jAppender;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
import org.springside.modules.utils.Threads;
|
||||
@@ -20,10 +17,6 @@ import org.springside.modules.utils.Threads;
|
||||
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
|
||||
public class JdkTimerJobTest extends SpringTxTestCase {
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
@Test
|
||||
public void scheduleJob() throws Exception {
|
||||
|
||||
|
||||
-7
@@ -3,13 +3,10 @@ package org.springside.examples.showcase.schedule;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springside.examples.showcase.data.SampleDataRule;
|
||||
import org.springside.modules.log.MockLog4jAppender;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
import org.springside.modules.utils.Threads;
|
||||
@@ -25,10 +22,6 @@ import org.springside.modules.utils.Threads;
|
||||
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
|
||||
public class QuartzTimerClusterJobTest extends SpringTxTestCase {
|
||||
|
||||
@Rule
|
||||
@Autowired
|
||||
public SampleDataRule sampleDataRule;
|
||||
|
||||
@Test
|
||||
public void scheduleJob() throws Exception {
|
||||
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package org.springside.modules.test.data;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
public class DefaultDataInitializer implements InitializingBean {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private String defaultDataFile;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public void setDefaultDataFile(String defaultDataFile) {
|
||||
this.defaultDataFile = defaultDataFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Fixtures.reloadData(dataSource, defaultDataFile);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user