我有一个简单的目标:我希望能够使用maven-failsafe-plugin,或者任何可行的替代方法,对我用maven-shade-plugin构建的jar运行测试。具体来说,我希望在shade运行后运行测试,因为我希望有一个集成测试来验证shade的重新定位没有破坏我试图重新定位的东西,因为它经常会破坏。
当我试图重新定位Jackson时,确保Jackson仍然能够在某些POJO上找到注解/等,以便它们正确地序列化(反序列化)是很重要的。显然,这在重新定位前是有效的。我们遇到了阴影jar没有正确地序列化(反序列化)的问题,所以对我们来说,在部署前进行某种测试来验证这种行为是很重要的。
我遇到的问题似乎是maven-failsafe-plugin在运行shaded jar的时候测试了原始源代码。这意味着,它无法加载我重新定位的一个类,尽管maven-shade-plugin中的重新定位过程应该已经(并且在live工件中确实)重新定位了对那个类的引用。
我的期望:maven-failsafe-plugin应该完全脱离阴影部分的源代码运行。如果没有,其他的东西应该允许我在build/CI时使用阴影部分/重新定位的代码运行一个类似的测试。例如,就像我从命令行运行它一样。顺便说一句,下面是我这样做得到的输出:
>java -jar shade-integration-tests-1.0-SNAPSHOT.jar
{key=123}
test.shaded.com.fasterxml.jackson.databind.ObjectMapper
我从测试中得到异常。注意,这不是Assert失败,而是类加载失败(没有发生其他异常):
Running test.shade.integration.tests.JsonIT
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.057 s <<< FAILURE! - in test.shade.integration.tests.JsonIT
test.shade.integration.tests.JsonIT.testClassName Time elapsed: 0.032 s <<< ERROR!
java.lang.NoSuchFieldError: MAPPER
at test.shade.integration.tests.JsonIT.testClassName(JsonIT.java:9)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92)
at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
...
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>shade-integration-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>test.shade.integration.tests.MainClass</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<relocations>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>test.shaded.com.fasterxml.jackson</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.12.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
MainClass.java (can大概忽略了这一点):
package test.shade.integration.tests;
import java.util.Map;
public class MainClass {
public static void main(String[] args) throws Exception {
Map<String, Object> result = Json.MAPPER.readValue("{\"key\":123}", Map.class);
System.out.println(result);
System.out.println(Json.MAPPER.getClass().getCanonicalName());
}
}
Json.java
package test.shade.integration.tests;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Json {
public static final ObjectMapper MAPPER = new ObjectMapper(); // This is part of the problem
private Json() {
}
}
JsonTest.java (maven-surefire插件,运行良好的junit测试):
package test.shade.integration.tests;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class JsonTest {
@Test
void testClassName() {
String result = Json.MAPPER.getClass().getCanonicalName();
assertEquals("com.fasterxml.jackson.databind.ObjectMapper", result);
}
}
JsonIT.java (maven-failsafe-plugin,失败的IT):
package test.shade.integration.tests;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class JsonIT {
@Test
void testClassName() {
String result = Json.MAPPER.getClass().getCanonicalName();
assertEquals("test.shaded.com.fasterxml.jackson.databind.ObjectMapper", result);
}
}
1条答案
按热度按时间polhcujo1#
问题是,如果您在同一个模块中运行测试,甚至在多模块Maven reactor中的依赖模块中运行测试,模块对要着色的类的依赖当然也总是在依赖列表中。因此,您希望隔离地运行IT,即不从同一个reactor中运行。最简单的方法是Maven Invoker Plugin。这在许多OSS项目中都是为了实现这一点而使用的。例如,请参见我正在参与的一个项目AspectJMavenPlugin中是如何完成的。