Spring boot之使用Junit测试

1,pom.xml引入junit依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <scope>testscope>
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进行测试


你可能感兴趣的:(spring-boot)