java - 无法运行带有嵌入式 Glassfish 的 Arquillian

标签 java maven netbeans glassfish jboss-arquillian

一些可能有用的信息:

  • 项目:基于maven的cdi
  • 环境:Ubuntu 16.04
  • JDK:Oracle 8
  • IDE:Netbeans 8.2

这是正在测试的 CDI Bean:

@Named
@SessionScoped
public class UserPreferencies implements Serializable {

private Locale currentLocale;

public UserPreferencies() {
    //locale is initiated to French but may be overriden in the
    //post-constructed method
    currentLocale = new Locale("fr");
}

@PostConstruct
public void initialize() {
    /**
     * if a user specific locale is provided in the browser
     * we should be setting it here
     */
    if(FacesContext.getCurrentInstance() != null){
        Optional<ExternalContext> optContext = ofNullable(FacesContext.getCurrentInstance()
                .getExternalContext());
        if (optContext.isPresent()) {
            Optional<Locale> userLocale = ofNullable(optContext.get().getRequestLocale());
            if (userLocale.isPresent()) {
                currentLocale = userLocale.get();
            }
        }
    }
}

public Locale getCurrentLocale() {
    return currentLocale;
}

public void setCurrentLocale(Locale currentLocale) {
    this.currentLocale = currentLocale;
}

}

这些是相关的 pom.xml 设置(按照 official arquillian guide 编写):

<dependencies>
    ...
    <!-- the support for the arquillian test framework -->
    <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.1.11.Final</version>
        <scope>import</scope>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <version>1.1.11.Final</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<profiles>
    <profile>
        <id>arquillian-glassfish-embedded</id>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian.container</groupId>
                <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                <version>1.0.0.CR4</version>
                <scope>test</scope>
                <exclusions>
                        <exclusion> <groupId>org.jboss.arquillian.container</groupId>
                            <artifactId>arquillian-container-spi</artifactId>
                        </exclusion>
                    </exclusions>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-all</artifactId>
                <version>3.1.2</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

然后我使用 NetBeans 的“设置配置”选项设置上述 Maven 配置文件。

这是我尝试运行的测试(如您所见,相当基本):

@RunWith(Arquillian.class)
public class UserPreferenciesTests {

public UserPreferenciesTests() {
}

//humble attempt to mock glassfish only with the UserPreferencies bean inside
@Deployment
public static JavaArchive createDeployment() {
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
            .addClass(UserPreferencies.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    System.out.println(jar.toString(true));
    return jar;
}

@Inject
UserPreferencies preferencies;

@Test
public void localeIsNotNull(){
    assertNotNull(preferencies.getCurrentLocale().getCountry());
}

@Test
public void defaultLocaleTest() {
    assertTrue("The default locale should be \"fr\"",
            "fr".equalsIgnoreCase(preferencies.getCurrentLocale().getLanguage()));
}
}

不幸的是,运行测试会产生以下堆栈跟踪:

Running com.vgorcinschi.rimmanew.cdi.UserPreferenciesTests
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.512 sec <<< FAILURE! - in com.vgorcinschi.rimmanew.cdi.UserPreferenciesTests
com.vgorcinschi.rimmanew.cdi.UserPreferenciesTests  Time elapsed: 0.51 sec  <<< ERROR!
java.lang.RuntimeException: Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:165)
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)
    at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:113)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:161)
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)
    at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:113)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: java.lang.NoSuchMethodError: org.jboss.shrinkwrap.descriptor.api.DescriptorImporter.fromString(Ljava/lang/String;)Lorg/jboss/shrinkwrap/descriptor/api/Descriptor;
    at org.jboss.arquillian.config.impl.extension.ConfigurationSysPropResolver.resolveSystemProperties(ConfigurationSysPropResolver.java:55)
    at org.jboss.arquillian.config.impl.extension.ConfigurationRegistrar.loadConfiguration(ConfigurationRegistrar.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
    at org.jboss.arquillian.core.impl.ManagerImpl.start(ManagerImpl.java:290)
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.<init>(EventTestRunnerAdaptor.java:63)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:161)
    at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)
    at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:113)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)


Results :

Tests in error: 
  UserPreferenciesTests.com.vgorcinschi.rimmanew.cdi.UserPreferenciesTests » Runtime

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

------------------------------------------------------------------------
BUILD FAILURE

关于这个问题的任何建议或分享的专业知识都值得赞赏,因为尽管我在 arquillian 网站上进行了大量阅读,但我对 Arquillian 的实际测试还相当陌生。

[更新] 执行 mvn dependency:tree -Dverbose 时,我发现新库存在依赖冲突,导致新库被忽略(我更新了上面的 pom 文件)。这是新的错误(项目中确实缺少org.jvnet.hk2.component.MultiMap,但是我排除的库没有被删除 - 它已被正确的版本替换... ):

com.vgorcinschi.rimmanew.cdi.UserPreferenciesTests  Time elapsed: 
0.666 sec  <<< ERROR!
java.lang.NoSuchMethodError: org.jvnet.hk2.component.MultiMap.<init>(Z)V
    at org.jvnet.hk2.component.Habitat.<init>(Habitat.java:127)
    at org.jvnet.hk2.component.Habitat.<init>(Habitat.java:120)
    at com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl.newHabitat(AbstractModulesRegistryImpl.java:118)
    at com.sun.enterprise.module.bootstrap.Main.createHabitat(Main.java:444)
    at com.sun.enterprise.glassfish.bootstrap.StaticGlassFishRuntime.newGlassFish(StaticGlassFishRuntime.java:104)
    at org.jboss.arquillian.container.glassfish.embedded_3_1.GlassFishContainer.setup(GlassFishContainer.java:147)
    at org.jboss.arquillian.container.glassfish.embedded_3_1.GlassFishContainer.setup(GlassFishContainer.java:67)
    at org.jboss.arquillian.container.impl.ContainerImpl.setup(ContainerImpl.java:181)
    at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController$7.perform(ContainerLifecycleController.java:149)
    at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController$7.perform(ContainerLifecycleController.java:145)
    at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController.forContainer(ContainerLifecycleController.java:255)
    at org.jboss.arquillian.container.impl.client.container.ContainerLifecycleController.setupContainer(ContainerLifecycleController.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

...

最佳答案

我终于明白了。我将其发布,以防这对遇到类似问题的其他人有用。所有问题都来自于相互冲突的依赖关系,但其中许多问题是看不见的,因为我使用了 glassfish-embedded-all(以及后来的 payara-embedded-all 作为其替代品)所提供的范围。我开始一一删除引发这些错误的依赖项。例如,旧的 hk2 依赖项来自 org.glassfish.jersey.core:jersey-client:2.21

然后我在 org.apache.deltaspike.core:deltaspike-core-impl:1.01 上遇到了类似的问题。这样还有另一个问题 - 即它被 cdi-unit 框架在一个测试方法中使用。考虑到 cdi 单元和 arquillian 执行相同的操作,但后者功能更强大,我通过排除该测试方法完成了操作。

这是对我有用的 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>

...

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.3.1.RELEASE</spring.version>
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.21</version>
        <!-- important hk2 were excluded because they shadowed the provided
        arquillian dependencies with higher version. Should the hk2 library be needed
        add this artifact: org.glassfish.hk2:hk2:2.5.0-b32       -->
        <exclusions>
            <exclusion>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-locator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.21</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.4</version>
        <scope>test</scope>
    </dependency>
    ...
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>    
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jglue.cdi-unit</groupId>
        <artifactId>cdi-unit</artifactId>
        <version>3.1.3</version>
        <exclusions>                
            <exclusion>
                <groupId>org.jboss.weld.se</groupId>
                <artifactId>weld-se-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.easymock</groupId>
                <artifactId>easymock</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.deltaspike.core</groupId>
                <artifactId>deltaspike-core-impl</artifactId>
            </exclusion>
        </exclusions>
        <scope>test</scope>                          
    </dependency>        
    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se-core</artifactId>
        <version>2.2.2.Final</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <!-- the support for the arquillian test framework -->         
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency> 
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
        <version>1.0.0.CR4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>fish.payara.extras</groupId>
        <artifactId>payara-embedded-all</artifactId>
        <version>4.1.1.161</version>
        <scope>test</scope>
    </dependency>       
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.11.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>       
    </dependencies>
</dependencyManagement>    
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.9</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>   
...

另请注意,我已经更新了 java 文件(在问题中)以改进测试的逻辑部分。

关于java - 无法运行带有嵌入式 Glassfish 的 Arquillian,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334791/

相关文章:

java - this.getClass().getResource().openStream ("file.xls") 返回 NullPointerException (MAVEN 项目)

maven - Gradle - 执行所有 GenerateMavenPom 任务的自定义任务

java - 在netbeans中安装定时器组件?

java - @exceptionhandler 不在 Spring REST 中工作

java - 以编程方式单击 swt 中的组合框按钮

java - 保存 Canvas 只保存背景,不保存绘图

java - "Error: Could not find or load main class com.mycompany.App"尝试执行 Maven 生成的 Jar

java - 如何将Sublime Text的UI主题获取到NetBeans?

java - Netbeans 无法启动 Tomcat

java - 如何使 Java 类中的方法返回 findViewById?