idea中 ctrl+shift+t 打开创建test
pom中引入必要的包 spring-test 和 junit
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
下面的具体的test实现
//首先指定Junit的Runner
@RunWith(SpringJUnit4ClassRunner.class)
//指明配置文件所在
@ContextConfiguration(locations={"classpath:spring-biz.xml"})
//指定事务管理器
//@Transactional(transactionManager="transactionManager") //用这个貌似测试完后 回滚了
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) //defaultRollback 测试完后是否回滚
//@WebAppConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class })
//继承AbstractTransactionalJUnit4SpringContextTests来获取Spring上下文环境来获取Bean
//我现在没有继承 AbstractTransactionalJUnit4SpringContextTests,也能获取bean,
//如果继承了这个,需要有DataSource,但JPA配置中不带这个,去掉了目前也没有发现不妥。
public class CheckInfoPageActionTest {
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(CheckInfoPageActionTest.class);
@Autowired
private CheckInfoBiz checkInfoBiz;
@Test
public void testTestXTDK() throws Exception {
logger.info("START");
Date d1 = new Date();
checkInfoBiz.checkSynchronous();
Date d2 = new Date();
logger.info("END:" + (d2.getTime() - d1.getTime()));
}
}