天天看點

SpringBoot - 內建 junit 測試類

基于 上一篇 文章, 我們要對完成的代碼進行測試, 需要開啟 SpringbootMybatisApplicationTests, 在浏覽器輸入對應的位址, 方可進行結果測試展示, 這裡, 我們通過 SpringBoot 內建 junit 測試類來完成另一種更簡單的方法進行測試

一. 導入 junit 測試依賴包

<!--測試的起步依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
           

二. 編寫測試類

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class MybatisTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){
        List<User> users = userMapper.userList();
        System.out.println(users);
    }

}
           

測試結果展示:

SpringBoot - 內建 junit 測試類

源代碼下載下傳: https://pan.baidu.com/s/1WGsdz49g1XmqhMdS0789Ww

繼續閱讀