1、Spring MVC框架引入依赖,SpringBoot(暂时不需要),version版本最好与项目中使用的Spring的版本一致
<!--Spring MVC-->
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.5.RELEASE</version> </dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
2、Spring MVC框架 测试文件引入基本标签
@WebAppConfiguration //主要重要load ApplicationContext @ContextConfiguration({"classpath:spring/spring.xml"}) //加载Spring @TestExecutionListeners(listeners = DependencyInjectionTestExecutionListener.class) //不加的话拿不到spring里面的东西 public class BaseTests extends AbstractTransactionalTestNGSpringContextTests {
@Autowired //可以引入controller、service、mapper等类
private LoginController loginController;
@Test public void test(String name,String passWord) {
loginController.login(name,passWord)
}
}
3、SpringBoot框架测试文件引入基本标签
@SpringBootTest
@SpringBootTest(classes = 你的Application.class)//加上这个,多个测试类不会重复加载spring上下文
@TestExecutionListeners(listeners = DependencyInjectionTestExecutionListener.class) public class BaseTests {
@Autowired //可以引入controller、service、mapper等类
private LoginController loginController;
@Test public void test(String name,String passWord) {
loginController.login(name,passWord)
}
}
ps:有问题可以提出来哦~ 顺带打个广告,自己的临安山核桃,谢谢各位大佬,https://m.tb.cn/h.eCwDIA3?sm=53be6e