使用Junit进行测试

pom.xml中添加依赖

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.6.RELEASE</version>
            <scope>provided</scope>  
</dependency>
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
</dependency>
<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>test</scope>
</dependency>    
//@RunWith就是一个运行器  
//@RunWith(JUnit4.class)就是指用JUnit4来运行 
//@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境 
//@RunWith(Suite.class)的话就是一套测试集合 
@RunWith(SpringJUnit4ClassRunner.class
) @ContextConfiguration(locations = "classpath:bean.xml")//加载配置文件 public class AccountServiceTest { @Autowired private IAccountService as; @Test public void testFindOne() { //3.执行方法 //ApplicationContext ac= new ClassPathXmlApplicationContext ("bean.xml"); // as=ac.getBean ("accountService",IAccountService.class); Account account = as.findAccountById(1); System.out.println(account); } @Test public void testUpdate() { //3.执行方法 Account account = as.findAccountById(4); account.setMoney(23456f); as.updateAccount(account); } }

猜你喜欢

转载自www.cnblogs.com/mkl7/p/10685698.html