本文对 SpringBoot 集成 Junit 做简单小记。

1、添加依赖

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

2、编写测试类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootDemoApplication.class)
class SpringBootDemoApplicationTests {
    @Autowired
    private ScoreDao scoreDao;

    @Test
    void queryAllScoresTest() {
        List<Score> scores = scoreDao.queryAllScores();
        System.out.println(scores);
    }
}

3、测试运行

1
[Score(id=1, name=小明, subject=语文, score=92, create_time=Mon Feb 13 19:49:00 CST 2023, update_time=Mon Feb 13 20:03:48 CST 2023)]