GVKun编程网logo

使用JUnit 5进行spring-boot-starter-test(spring boot junit5)

21

如果您想了解使用JUnit5进行spring-boot-starter-test的相关知识,那么本文是一篇不可错过的文章,我们将对springbootjunit5进行全面详尽的解释,并且为您提供关于7

如果您想了解使用JUnit 5进行spring-boot-starter-test的相关知识,那么本文是一篇不可错过的文章,我们将对spring boot junit5进行全面详尽的解释,并且为您提供关于7. Spring Boot Starter 的介绍及使用?java 版 SpringCloud+SpringBoot+uniapp+vue b2b2c 微服务商城 微服务电商 直播带货源码、java – 使用动态值覆盖Junit Test中的默认Spring-Boot application.properties设置、justauth-spring-boot-starter 1.3.0 发布、justauth-spring-boot-starter V1.3.5 发布成功的有价值的信息。

本文目录一览:

使用JUnit 5进行spring-boot-starter-test(spring boot junit5)

使用JUnit 5进行spring-boot-starter-test(spring boot junit5)

spring-boot-starter-test从2.0.6版本开始使用,会引入JUnit 4依赖关系。我如何使用spring-boot-starter-test(通过Gradle)使用JUnit 5,而又不引入JUnit 4依赖关系?

如果有帮助,这是Gradle依赖输出的一部分:

+--- org.springframework.boot:spring-boot-starter-test -> 2.0.5.RELEASE|    +--- org.springframework.boot:spring-boot-starter:2.0.5.RELEASE (*)|    +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE|    |    \--- org.springframework.boot:spring-boot:2.0.5.RELEASE (*)|    +--- org.springframework.boot:spring-boot-test-autoconfigure:2.0.5.RELEASE|    |    +--- org.springframework.boot:spring-boot-test:2.0.5.RELEASE (*)|    |    \--- org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE (*)|    +--- com.jayway.jsonpath:json-path:2.4.0|    |    +--- net.minidev:json-smart:2.3|    |    |    \--- net.minidev:accessors-smart:1.2|    |    |         \--- org.ow2.asm:asm:5.0.4|    |    \--- org.slf4j:slf4j-api:1.7.25|    +--- junit:junit:4.12|    |    \--- org.hamcrest:hamcrest-core:1.3

这是我的 build.gradle 文件:

buildscript {    ext {        springBootVersion = ''2.0.6.RELEASE''    rootGradleDir = "${rootProject.rootDir}/gradle"    }    repositories {        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")    }}apply plugin: ''java''apply plugin: ''idea''apply plugin: ''org.springframework.boot''apply plugin: ''io.spring.dependency-management''apply from: "${rootGradleDir}/staticCodeAnalysis.gradle"group = ''com.example''version = ''0.0.1-SNAPSHOT''sourceCompatibility = 1.8repositories {    mavenCentral()}test {  useJUnitPlatform()}dependencies {    implementation(''org.springframework.boot:spring-boot-starter-data-jpa'')    implementation(''org.springframework.boot:spring-boot-starter-jdbc'')    implementation(''org.springframework.boot:spring-boot-starter-security'')    implementation(''org.springframework.boot:spring-boot-starter-thymeleaf'')    implementation(''org.springframework.boot:spring-boot-starter-validation'')    implementation(''org.springframework.boot:spring-boot-starter-web'')    implementation(''org.liquibase:liquibase-core'')    runtimeOnly(''org.springframework.boot:spring-boot-devtools'')  runtimeOnly(''org.postgresql:postgresql'')  testImplementation(''org.springframework.boot:spring-boot-starter-test'')  testImplementation(''org.springframework.security:spring-security-test'')  implementation(''org.glassfish.jaxb:jaxb-runtime:2.3.1'')  implementation(''org.glassfish.jaxb:jaxb-runtime2.3.1'')  implementation(''org.springframework.boot:spring-boot-starter-data-redis'')  testCompile(''org.junit.jupiter:junit-jupiter-api:5.3.1'')  testCompile(''org.junit.jupiter:junit-jupiter-params:5.3.1'')  testRuntime(''org.junit.jupiter:junit-jupiter-engine:5.3.1'')}

更新

添加JUnit 5依赖项并执行注释中提到的exclude可以解决问题。现在,测试依赖项如下所示:

testImplementation(''org.springframework.boot:spring-boot-starter-test'') {    exclude group: ''junit'', module: ''junit'' //by both name and group}

答案1

小编典典

从Gradle 4.6开始(我相信),已经有本机JUnit 5支持。您可以仅包含JUnit5,如下所示:

dependencies {  testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"  testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"  testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"}

您还需要:

test {  useJUnitPlatform()}

JUnit
4和5使用不同的包名称,因此它们可以共存于同一项目中。许多注释是相同的(@Test,等),因此请确保从org.junit.jupiter.api包装中包括它们。

7. Spring Boot Starter 的介绍及使用?java 版 SpringCloud+SpringBoot+uniapp+vue b2b2c 微服务商城 微服务电商 直播带货源码

7. Spring Boot Starter 的介绍及使用?java 版 SpringCloud+SpringBoot+uniapp+vue b2b2c 微服务商城 微服务电商 直播带货源码

Spring Boot 的便利性体现在,它简化了很多烦琐的配置,这对于开发人员来说是一个福音,通过引入各种 Spring Boot Starter 包可以快速搭建出一个项目的脚手架。

目前提供的 Spring Boot Starter 包有:

  • spring-boot-starter-web:快速构建基于 Spring MVC 的 Web 项目,使用 Tomcat 做默认嵌入式容器。
  • spring-boot-starter-data-redis:操作 Redis
  • spring-boot-starter-data-mongodb:操作 Mongodb。
  • spring-boot-starter-data-jpa:操作 Mysql。
  • spring-boot-starter-activemq:操作 Activemq。
  • ……


自动配置非常方便,当我们要操作 Mongodb 的时候,只需要引入 spring-boot-starter-data-mongodb 的依赖,然后配置 Mongodb 的链接信息 spring.data.mongodb.uri=mongodb://localhost/test 就可以使用 MongoTemplate 来操作数据,MongoTemplate 的初始化工作全部交给 Starter 来完成。

自动配置麻烦的是当出现错误时,排查问题的难度上升了。自动配置的逻辑都在 Spring Boot Starter 中,要想快速定位问题,就必须得了解 Spring Boot Starter 的内部原理。接下来我们自己动手来实现一个 Spring Boot Starter。

Spring Boot Starter 项目创建

创建一个项目 spring-boot-starter-demo,pom.xml 配置代码如下所示。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

创建一个配置类,用于在属性文件中配置值,相当于 spring.data.mongo 这种形式,代码如下所示。

import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;

@Data
@ConfigurationProperties("spring.user")
public class UserPorperties {
    private String name;
}

再定义一个 Client,相当于 MongoTemplate,里面定一个方法,用于获取配置中的值,代码如下所示。

public class UserClient {
    private UserPorperties userPorperties;

    public UserClient() {

    }

    public UserClient(UserPorperties p) {
        this.userPorperties = p;
    }

    public String getName() {
        return userPorperties.getName();
    }
}

自动创建客户端

一个最基本的 Starter 包定义好了,但目前肯定是不能使用 UserClient,因为我们没有自动构建 UserClient 的实例。接下来开始构建 UserClient,代码如下所示。

@Configuration
@EnableConfigurationProperties(UserPorperties.class)
public class UserAutoConfigure {

    @Bean
    @ConditionalOnProperty(prefix = "spring.user", value = "enabled", havingValue = "true")
    public UserClient userClient(UserPorperties userPorperties) {
        return new UserClient(userPorperties);
    }
}

Spring Boot 会默认扫描跟启动类平级的包,假如我们的 Starter 跟启动类不在同一个主包下,如何能让 UserAutoConfigure 生效?

在 resources 下创建一个 META-INF 文件夹,然后在 META-INF 文件夹中创建一个 spring.factories 文件,文件中指定自动配置的类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.cxytiandi.demo.UserAutoConfigure

Spring Boot 启动时会去读取 spring.factories 文件,然后根据配置激活对应的配置类,至此一个简单的 Starter 包就实现了。

使用 Starter

现在可以在其他的项目中引入这个 Starter 包,代码如下所示。

<dependency>
    <groupId>com.cxytiandi</groupId>
    <artifactId>spring-boot-starter-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

引入之后就直接可以使用 UserClient,UserClient 在项目启动的时候已经自动初始化好,代码如下所示。

@RestController
public class UserController {

    @Autowired
    private UserClient userClient;

    @GetMapping("/user/name")
    public String getUserName() {
        return userClient.getName();
    }
}

属性文件中配置 name 的值和开启 UserClient:

spring.user.name=zhangsan
spring.user.enabled=true

访问 /user/name 就可以返回我们配置的 zhangsan。

使用注解开启 Starter 自动构建

很多时候我们不想在引入 Starter 包时就执行初始化的逻辑,而是想要由用户来指定是否要开启 Starter 包的自动配置功能,比如常用的 @EnableAsync 这个注解就是用于开启调用方法异步执行的功能。

同样地,我们也可以通过注解的方式来开启是否自动配置,如果用注解的方式,那么 spring.factories 就不需要编写了,下面就来看一下怎么定义启用自动配置的注解,代码如下所示。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({UserAutoConfigure.class})
public @interface EnableUserClient {

}

这段代码的核心是 @Import({UserAutoConfigure.class}),通过导入的方式实现把 UserAutoConfigure 实例加入 SpringIOC 容器中,这样就能开启自动配置了。

使用方式就是在启动类上加上该注解,代码如下所示。

@SpringBootApplication
public class SpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

使用配置开启 Starter 自动构建

在某些场景下,UserAutoConfigure 中会配置多个对象,对于这些对象,如果不想全部配置,或是想让用户指定需要开启配置的时候再去构建对象,这个时候我们可以通过 @ConditionalOnProperty 来指定是否开启配置的功能,代码如下所示。

@Bean
@ConditionalOnProperty(prefix = "spring.user",value = "enabled",havingValue = "true")
public UserClient userClient(UserPorperties userPorperties) {
    return new UserClient(userPorperties);
}

通过上面的配置,只有当启动类加了 @EnableUserClient 并且配置文件中 spring.user.enabled=true 的时候才会自动配置 UserClient。

配置 Starter 内容提示

在自定义 Starter 包的过程中,还有一点比较重要,就是对配置的内容项进行提示,需要注意的是,Eclipse 中是不支持提示的,Spring Tools 4 for Eclipse 中可以提示。

定义提示内容需要在 META-INF 中创建一个 spring-configuration-metadata.json 文件,代码如下所示。

{ "properties": [ { "name": "spring.user.name", "defaultValue": "cxytinadi" }, { "name": "spring.user.enabled", "type": "java.lang.Boolean", "defaultValue": false } ] }
  • name:配置名
  • type:配置的数据类型
  • defaultValue:默认值

推荐布式微服务商城

java – 使用动态值覆盖Junit Test中的默认Spring-Boot application.properties设置

java – 使用动态值覆盖Junit Test中的默认Spring-Boot application.properties设置

我想覆盖测试中application.properties中定义的属性,但@TestPropertySource只允许提供预定义的值.

我需要的是在随机端口N上启动服务器,然后将此端口传递给spring-boot应用程序.端口必须是短暂的,以允许同时在同一主机上运行多个测试.

我不是指嵌入式http服务器(jetty),而是在测试开始时启动的一些不同的服务器(例如zookeeper)和正在测试的应用程序必须连接到它.

实现这一目标的最佳方法是什么?

(这是一个类似的问题,但答案没有提到短暂端口的解决方案 – Override default Spring-Boot application.properties settings in Junit Test)

最佳答案
您可以像这样覆盖@BeforeClass中port属性的值:

@BeforeClass
public static void beforeClass() {
    System.setProperty("zookeeper.port",getRandomPort());
}

justauth-spring-boot-starter 1.3.0 发布

justauth-spring-boot-starter 1.3.0 发布

justauth-spring-boot-starter 1.3.0 发布

justauth-spring-boot-starter是单独为SpringBoot项目开发的一款starter包。SpringBoot项目可以直接使用该依赖进行集成JustAuth

更新信息:

  • 修改 groupId 为 com.xkcoding.justauth
  • 升级 JustAuth 版本:1.13.0,支持 推特

项目地址:https://github.com/justauth/justauth-spring-boot-starter

justauth-spring-boot-starter V1.3.5 发布成功

justauth-spring-boot-starter V1.3.5 发布成功

justauth-spring-boot-starter V1.3.5 发布成功

关于 justauth-spring-boot-starter

justauth-spring-boot-starter 是 Spring Boot 集成 JustAuth 的最佳实践。如果你的项目是基于 Spring Boot 构建,则推荐你用 justauth-spring-boot-starter ,源码地址:justauth-spring-boot-starter

justauth-spring-boot-starter 是由在 github 已获 20k 关注度的深度学习并实战  springboot 项目的作者(xkcoding)提供,  项目地址,spring-boot-demo

论小白上手 Spring Boot、老手深度学习 Spring Boot,spring-boot-demo 最适合!

使用帮助

基础配置

  • 引用依赖
<dependency>    
  <groupId>com.xkcoding.justauth</groupId>    
  <artifactId>justauth-spring-boot-starter</artifactId>    
  <version>1.3.5</version>
</dependency>
  • 添加配置,在 application.yml 中添加配置配置信息

注意:

  • justauth.type节点的配置,请根据项目实际情况选择,多余的可以删除
  • 如果使用 QQ 登录,并且需要获取unionId,则必须传union-id配置,并置为true
  • 如果使用支付宝登录,必传alipay-public-key
  • 如果使用 Stack Overflow 登录,必传stack-overflow-key
  • 如果使用企业微信登录,必传agent-id
  • 如果使用 CODING 登录,必传coding-group-name

更多使用帮助,请参考 README

justauth-spring-security-starter

justauth-spring-security-starter: 1.1.12 也已由社区小伙伴发布完成。 justauth-spring-security-starter 是 Spring security 集成 JustAuth 实现第三方授权登录脚手架。项目地址:justauth-spring-boot-security-starter。 该项目 

  • 支持所有 justAuth 支持的第三方登录,登录后自动注册 或 绑定.
  • 支持定时刷新 accessToken, 支持分布式定时任务, 
  • 支持第三方授权登录的用户信息表与 token 信息表的缓存功能. 
  • 支持第三方绑定与解绑及查询接口。

关于 Just Auth

JustAuth,如你所见,它仅仅是一个第三方授权登录工具类库,它可以让我们脱离繁琐的第三方登录SDK,让登录变得So easy!

目前已支持Github、Gitee、微博、钉钉、百度、Coding、腾讯云开发者平台、OSChina、支付宝、QQ、微信、淘宝、Google、Facebook、抖音、领英、小米、微软、今日头条、Teambition、StackOverflow、Pinterest、人人、华为、企业微信、酷家乐、Gitlab、美团、饿了么和推特等第三方平台的授权登录。 Login, so easy!

部分用户

image.png

特点

废话不多说,就俩字:

  1. :已集成十多家第三方平台(国内外常用的基本都已包含),仍然还在持续扩展中([开发计划]([开发计划] 待扩展的第三方平台 · Issue #IUGRK · yadong.zhang/JustAuth - Gitee.com))!
  2. :API就是奔着最简单去设计的,尽量让您用起来没有障碍感!

项目源码库

  • https://gitee.com/yadong.zhang/JustAuth
  • https://github.com/justauth/JustAuth

本文由博客群发一文多发等运营工具平台 OpenWrite 发布

关于使用JUnit 5进行spring-boot-starter-testspring boot junit5的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于7. Spring Boot Starter 的介绍及使用?java 版 SpringCloud+SpringBoot+uniapp+vue b2b2c 微服务商城 微服务电商 直播带货源码、java – 使用动态值覆盖Junit Test中的默认Spring-Boot application.properties设置、justauth-spring-boot-starter 1.3.0 发布、justauth-spring-boot-starter V1.3.5 发布成功的相关信息,请在本站寻找。

本文标签: