Spring boot之使用Junit测试

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)和@SringBootTest
3,使用@Autowired或@Resource注入bean进行测试


猜你喜欢

转载自blog.csdn.net/onepiecemonkey/article/details/79402868