提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一、整合Junit
测试时当测试的包名和方法的报名相同时,不用加入
@SpringBootTest(classes = SpringbootTestApplication.class)
不用加入后面(classes = SpringbootTestApplication.class)
直接写@springboottest就可以
但包名不同时,需要加入@SpringBootTest(classes = SpringbootTestApplication.class)
在整合Junit时
在测试类中@RunWith(SpringRunner.class)出错的话
在pom.xml中导入依赖即可
1,使用idea快速构建springboot项目
2,编写UserService类
package com.itheima.springboottest;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add(){
System.out.println("add ...");
}
}
3.编写测试类
//不同包名
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testadd(){
userService.add();
}
}
//同包名
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testadd(){
userService.add();
}
}
总结
主要是注意包名是否相同,
相同时写@SpringBootTest,
不同时写@SpringBootTest(classes = SpringbootTestApplication.class)
写错的话@Autowired,下的会出错
纯纯笔记