Spring 整合JUnit,测试
-
pom.xml
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.10.RELEASE</version> </dependency>
-
测试类:
package com.itheima.service; import com.itheima.config.MybatisConfig; import com.itheima.config.SpringConfig; 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; //设定Spring整合junit的专用类运行器 @RunWith(SpringJUnit4ClassRunner.class) //因为用spring所以得把spring环境告诉它 @ContextConfiguration(classes = {SpringConfig.class}) public class AccountServiceTest { //需要测谁,就把谁设成属性 @Autowired private AccountService accountService; @Test public void testSave(){ System.out.println(accountService.findById(1)); } @Test public void testFindAll(){ System.out.println(accountService.findAll()); } }