SpringBoot中如何利用Junit实现单元测试(junit,springboot,编程语言)

时间:2024-05-02 01:26:27 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    SpringBoot%E4%B8%AD%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8Junit%E5%AE%9E%E7%8E%B0%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95

1.需要在pom.xml中引入spring-boot-starter-test

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId></dependency>

2.单元测试代码

两个关键的注解

@RunWith(SpringRunner.class)

@SpringBootTest

package com.niugang.test;import java.util.List;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.niugang.entity.User;import com.niugang.service.UserService;

@RunWith(SpringRunner.class)

@SpringBootTestpublic class TestUser {

@Resource private UserService userService;

@Testpublic void test1(){List<User> list = userService.queryList(null);System.out.println("集合大小:"+list.size());}}

本文:SpringBoot中如何利用Junit实现单元测试的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:SpringBoot中如何自定义Banner下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18