Spring框架集成单元测试
1、添加jar包坐标
https://mvnrepository.com/artifact/org.springframework/spring-test https://mvnrepository.com/artifact/junit/junit/4.12
<!-- spring单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.12.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
注:有时候会出现jar包冲突,所以版本不一定最高,合适就行。 jar包版本冲突: java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith’s ‘value’ attribute or make the default bootstrapper class available. mapper接口和mapper.xml文件没有映射起来: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
2、创建测试类并添加注解
package edu.pro.wenhua;
import edu.pro.wenhua.bean.Login;
import edu.pro.wenhua.service.LoginService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class TestDemo {
@Autowired
LoginService loginService;
@Test
public void TestLogin(){
Login login = loginService.SelectLogin();
System.out.println(login);
}
}
单元测试在开发中是十分重要的,所以必须掌握和熟练;愿与君共勉,不负韶华。
|