场景如下:
系统有很多Spring的注解之类的,包括初始化等,那么此时写单元测试就需要利用AbstractTransactionalJUnit4SpringContextTests来实现了,代码如下:
@ContextConfiguration(locations={
"file:///D:/workSource/splatform/target/splatform/WEB-INF/dispatcher-servlet.xml",
"file:///D:/workSource/splatform/target/splatform/WEB-INF/spring/applicationContext*.xml"})
public class TestService extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
IRolesService IRolesService;
@Test
public void testM(){
try {
Roles entity=new Roles();
entity.setName("name");
entity.setDescription("desc");
IRolesService.saveRoles(entity);
System.out.println(entity.getId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
bean都是注解产生的,同时初始化都是通过@Autowired实现的,那么首先要加载配置文件,也就是指定ContextConfiguration
可以是classpath 也可以是绝对路径,通过file协议来实现,如:
file:///D:/workSource/splatform/target/splatform/WEB-INF/dispatcher-servlet.xml