maven坐标:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
代码:
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:applicationContext.xml")
@ContextConfiguration(classes = "具体的配置类类名字")
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSave(){
User user = new User();
user.setUserName("Jock");
user.setPassword("root");
user.setRealName("Jockme");
user.setGender(1);
user.setBirthday(new Date(333333000000L));
userService.save(user);
}
}
不做过多解释,粘贴即可!我自己记者不住这个注解!自用模板!
SpringBoot
注意SpringBoot的版本,在高版本需要我们自己引入junit低版本的坐标,高版本的没有@RunWith注解,在junit4.2以后版本没有该注解,SpringBoot的test版本越高,里面的junit越高,这个看情况改一下就ok的!
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = 启动类字节码文件.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void test() {
}
}