spring - 我应该如何在 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
- spring-boot-starter-web
- spring-boot-starter-data-rest
- spring-boot-starter-数据-jpa
- spring-boot-starter-data-jdbc
- spring-boot-starter-tomcat(不确定我是否需要这个)
解决方案
推荐阅读
- javascript - 如何在满足特定条件时销毁 React 组件的实例
- laravel - Laravel 与 Vuejs 和 Redis , laravel-echo , socketio-client
- python - 如何使 Tkinter 项目可下载?-
- python - 包含 \n 和 \t 的 Python 格式字符串
- sqlite - 具有不同列的 Sqlite UNION ALL 查询
- python - 如何使用 matplolib.animation 对单个数据点颜色的变化进行动画处理?
- react-native - 如何在 React Native 中使用 FlatList 滚动到按钮上的下一个项目?
- quarkus - 将 PanacheEntity 与 Quarkus "import.sql" 一起使用时如何避免 id 冲突(违反唯一约束)
- python - 使用python获取英语的英语语音表示
- python - LibreOffice 将 .docx 并行转换为 .pdf 效果不佳