带docker容器的Spring boot集成测试多模块maven应用程序
我有一个使用Spring引导的多模块maven应用程序。它定义了RESTAPI来为消费者提供服务带docker容器的Spring boot集成测试多模块maven应用程序,maven,docker,spring-boot,integration-testing,Maven,Docker,Spring Boot,Integration Testing,我有一个使用Spring引导的多模块maven应用程序。它定义了RESTAPI来为消费者提供服务 spring启动父级 myproject父项(父项和模块pom) 模块1 模块it(集成测试) 在我的模块1中,pom中的Spring boot maven插件配置如下 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-m
- spring启动父级
- myproject父项(父项和模块pom)
- 模块1
- 模块it(集成测试)
- myproject父项(父项和模块pom)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.4.RELEASE</version>
<configuration>
<mainClass>com.package.Application</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
IntegrationTest执行良好,能够测试REST API。然而,当我尝试使用module1jar文件运行docker运行docker文件时,spring引导并没有启动。它说找不到清单。下面是错误消息
在maven/sample-model-1.0.0-SNAPSHOT.jar中没有主清单属性。
当我从模块1中删除exec分类器时,当执行docker run命令时,spring引导能够启动。但是,如果分类器不存在,我在执行带有Spring引导测试的集成测试用例时会遇到问题
请您建议是否有任何选择。我认为,如果要使用分类器为Spring Boot生成单独的重新打包jar,那么您需要在docker maven插件配置中引用重新打包的jar。也就是说,您的
应该调用sample-model-1.0.0-SNAPSHOT-exec.jar。(注意“-exec”)
此外,对于良好的实践,您可能需要考虑为JAR目标使用项目变量,而不是对该版本进行硬编码,例如:
<entrypoint>
<exec>
<arg>java</arg>
<arg>-jar</arg>
<arg>maven/${project.artifactId}-${project.version}-exec.jar</arg>
</exec>
</entrypoint>
JAVA
-罐子
maven/${project.artifactId}-${project.version}-exec.jar
您还需要将程序集从带有dependencies descriptorRef的工件更改为内联程序集,以便将jar的“exec”版本内置到Docker映像中。(还请注意,具有依赖项的工件将所有这些冗余依赖项抛入到图像中,使其比需要的更大。)
目标
.
0644
${project.artifactId}-${project.version}-exec.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
<groups>io.sample.test.IntegrationTest</groups>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
@RunWith(SpringRunner.class)
@Category(IntegrationTest.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ControllerIT
{
@Autowired
private MockMvc mockMvc;
@Autowired
private Environment env;
@Test
public void IntegrationTestForService() throws Exception {
JsonNode node=mapper.readTree(new File("/src/integration-test/resources/Request.json"));
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/api/sample/v1")
.accept(MediaType.APPLICATION_JSON).content(node.toString())
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.CREATED.value(), response.getStatus());
}
}
<entrypoint>
<exec>
<arg>java</arg>
<arg>-jar</arg>
<arg>maven/${project.artifactId}-${project.version}-exec.jar</arg>
</exec>
</entrypoint>
<assembly>
<inline>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>.</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>${project.artifactId}-${project.version}-exec.jar</include>
</includes>
</fileSet>
</fileSets>
</inline>
</assembly>