1,pom.xml引入junit依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>2,编写测试类
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class UserServiceTest { @Resource private UserRepository userRepository; @Test public void testFindOne(){ // System.out.println(userRepository); System.out.println(userRepository.findByUserId(1)); } }
需要在测试类上添加@RunWith(SpringJUnit4ClassRunner.class)和@SringBootTest3,使用@Autowired或@Resource注入bean进行测试
本文介绍如何在Spring Boot项目中使用JUnit进行单元测试。首先通过pom.xml引入spring-boot-starter-test依赖,接着创建测试类并使用@RunWith和@SpringBootTest注解进行配置。最后通过@Autowired或@Resource注入需要测试的服务类。
4771

被折叠的 条评论
为什么被折叠?



