简体   繁体   English

如何配置最新的 Spring Boot 应用程序以在启动时运行单元测试

[英]How to configure latest Spring Boot application to run unit tests at startup

Recently I started working on demo Spring Boot application and I was unable to run the unit test when starting the application.最近我开始研究演示 Spring Boot 应用程序,但在启动应用程序时无法运行单元测试。 The purpose of this approach is we need to run all unit tests before starting the application.这种方法的目的是我们需要在启动应用程序之前运行所有单元测试。

Code of the Sample Demo Application is as follows.示例演示应用程序的代码如下。

Main class DemoApplication,主类DemoApplication,

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

Model class School,示范班学校,

package com.example.demo;

import org.springframework.stereotype.Component;

@Component
public class School {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Main class Unit test,主类单元测试,

package com.example.demo;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private School school;

    @Test
    void contextLoads() {
        school.setName("test");
        String schoolName = school.getName();
        Assertions.assertThat(schoolName).isNotNull();
    }

    @Test
    void main() {
        DemoApplication.main(new String[]{});
        school.setName("test");
        String schoolName = school.getName();
        Assertions.assertThat(schoolName).isNotNull();
    }

}

School Unit test,学校单元测试,

package com.example.demo;

import org.junit.Assert;
import org.junit.Test;

public class SchoolTests {

    @Test
    public void getTodoTest() {
        School school = new School();
        String name = "test";
        school.setName(name);
        Assert.assertEquals(name, school.getName());
    }

}

pom.xml file, pom.xml 文件,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

What are the additional configurations that need to be done to run the unit tests at the startup?在启动时运行单元测试需要做哪些额外的配置?

If you want to do this via Intellij, you can create a Run Configuration that runs the verify and spring-boot:run goals:如果您想通过 Intellij 执行此操作,您可以创建一个运行配置来运行verifyspring-boot:run目标:

在此处输入图片说明

As other people have said, this is something you would only want to do while developing.正如其他人所说,这是您在开发时只想做的事情。

Some suggestions:一些建议:

  • let's separate your local development and release, deployment procedure: use some kind of CI/CD process让我们将您的本地开发和发布、部署过程分开:使用某种 CI/CD 过程
  • you can build, run your unit tests and other static code analyzers is your CI process (for instance in gitlab-ci)你可以构建、运行你的单元测试和其他静态代码分析器是你的 CI 过程(例如在 gitlab-ci 中)
  • when you develop your application you can run unit tests without running your application, and you can run your application without running unit tests in your local machine, it is more efficient.在开发应用程序时,您可以在不运行应用程序的情况下运行单元测试,并且可以在不运行本地计算机上的单元测试的情况下运行应用程序,这样效率更高。
  • when you finish your development issue you can push it to version control that can run your CI or CI/CD process.完成开发问题后,您可以将其推送到可以运行 CI 或 CI/CD 流程的版本控制。 If you work in a team I suggest to use some kind of branching/version control strategy like gitflow, trunk based development etc.如果您在团队中工作,我建议使用某种分支/版本控制策略,例如 gitflow、基于主干的开发等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM