<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.springmvc3.0 +ibatis +Junit4(添加org.springframework.test-3.0.5.RELEASE.jar和junit.jar)</span>
2.因为业务逻辑都在Service中,所以测试Service即可,如果想测试action,使用spring3.2更方便(以后再写)
a.方式1:
public class UserTest {
private EssayListService essayListService;
@Before
public void before() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring/applicationContext.xml" });//xml可以写多个
essayListService = (EssayListService) context.getBean("essayListService");
}
@Test
public void queryUser() {
System.out.println(((List<MinSysScoreBean>) essayListService.queryRecentlySysScoreByUId(602744)).get(0)
.toString());
}
}
b.方式2:
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)//继承AbstractTransactionalJUnit4SpringContextTests后的事务配置
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml" })
//或者使用file全路径:file:res/spring/applicationContext.xml或者"file:WebContent/WEB-INF/springMVCForm-servlet.xml"
//具体见http://lohasle.iteye.com/blog/1617929,有配置读取
public class JUnitDaoBase extends AbstractTransactionalJUnit4SpringContextTests {
//也可以不继承AbstractTransactionalJUnit4SpringContextTests,及不配置上边的@TransactionConfiguration
//如果继承,事务配置见(http://blog.csdn.net/funi16/article/details/8691575)
@Autowired
private EssayListService essayListService;
@Test
public void rrrTest() {
System.out.println(((List<MinSysScoreBean>) essayListService.queryRecentlySysScoreByUId(602744)).get(0)
.toString());
}
}
参考:http://developer.51cto.com/art/201207/349175.htm
3....