首页 > 解决方案 > 我应该如何在 Spring Boot 中运行前端集成测试?

问题描述

我有一个简单的 Spring Boot 项目,它的 Maven 结构如下:

project
  - backend (spring boot)
  - frontend (angular)

我配置了项目,因此 *IT.java 类在 maven 的集成测试阶段运行,他们正在使用 H2 数据库成功测试 Spring Boot REST API。

我正在尝试配置项目,以便与浏览器交互的黄瓜测试将在项目的集成测试阶段运行。黄瓜测试启动正常,但失败的原因很简单,spring boot 应用程序在启动时没有提供 angular 文件。

在 backend/src/main/resources/static 我有一个简单的 index.html 文件。当黄瓜测试运行时,它们会打开浏览器,我会看到该文件的内容。

在后端模块(war)pom中,我从角度构建中复制了dist内容......

    <build>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>some.package.Boot</mainClass>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${maven.war.plugin.version}</version>
            <configuration>
                <warName>${root.context}</warName>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <resources>
          <resource>
           <directory>../frontend/<appname>/dist/frontend</directory>
            <!--<targetPath>src/main/resources/static</targetPath>-->
            <targetPath>static</targetPath>
           </resource>
         </resources>
   </build>

在生成的 war 文件中,角度文件被打包到 WEB-INF/classes/static/ 中。

您会从上面注意到,我还尝试将角度代码复制到资源/静态中,但这会将文件放在目标/类/src/main/resources/static 中,所以这不是正确的方法!

然而,黄瓜测试在运行时仍然只能看到来自 src/main/resources/static 的 index.html 文件的内容。

可能我们可以将 Angular 应用程序移动到与 java/webapp 代码相同的 src 树中,但这不是一个选项。

我如何说服 Spring Boot 使用目标中的战争而不是它看起来在做什么并从源中提供服务内容?

版本:Java 11

Maven 战争插件 3.2.2

父 pom 中定义的 Springboot 版本为 2.1.3.RELEASE

标签: springmavenspring-bootintegration-testingspring-boot-maven-plugin

解决方案


推荐阅读