简体   繁体   English

无法使用嵌入式Glassfish运行Arquillian

[英]Cannot run Arquillian with an embedded Glassfish

Some possibly useful information: 一些可能有用的信息:

  • Project: maven based cdi 项目:基于Maven的CDI
  • Environment: Ubuntu 16.04 环境:Ubuntu 16.04
  • JDK: oracle 8 JDK:甲骨文8
  • IDE: Netbeans 8.2 IDE:Netbeans 8.2

Here's the CDI Bean under test: 这是受测试的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;
}

}

These are pertinent pom.xml settings (written following the official arquillian guide ): 这些是相关的pom.xml设置(根据官方Arquillian指南编写):

<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>

Then I set the mentioned above maven profiles using NetBeans' 'Set Configuration' option. 然后,使用NetBeans的“设置配置”选项设置上述的Maven配置文件。

This is the test that I am trying to run (fairly basic as you may see): 这是我要运行的测试(您可能会看到的很基本):

@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()));
}
}

Unfortunatelly running the test yields the following stacktrace: 不幸地,运行测试会产生以下堆栈跟踪:

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

Any advice or shared expertise on this issue are appreciated as, although I have done a lot of reading on the arquillian site, I am fairly new to testing with Arquillian on the practical side. 尽管在Arquillian网站上进行了大量阅读,但我还是很实用的与Arquillian进行测试的新手,尽管在此问题上有任何建议或共同的专业知识,我们还是感激不尽。

[Update] Performing mvn dependency:tree -Dverbose I figured one dependency conflict for the new libraries that caused a newer library to be omitted (I updated the pom file above). [更新]执行mvn dependency:tree -Dverbose我发现了新库的一个依赖项冲突,导致新的库被忽略(我更新了上面的pom文件)。 This is the new error ( org.jvnet.hk2.component.MultiMap is indeed missing from the project, but the library that I excluded wasn't deleted - it as replaced by the correct version...): 这是新的错误( org.jvnet.hk2.component.MultiMap中确实缺少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)

... ...

Finally I've figured it out. 终于我明白了。 I am posting it in case this could be useful to other people experiencing similar issues. 我发布它是为了对其他遇到类似问题的人有用。 All problems were comming from the conflicting dependencies, but many of these were unseen because I used glassfish-embedded-all (and later payara-embedded-all as its replacement) with the scope provided. 所有问题都来自于相互冲突的依赖关系,但是许多问题是看不见的,因为我使用了glassfish-embedded-all (后来替换为payara-embedded-all )替换了提供的范围。 One by one I started removing the dependencies that were throwing these errors. 我一个接一个地开始删除引发这些错误的依赖项。 For example the old hk2 dependency was coming from org.glassfish.jersey.core:jersey-client:2.21 . 例如,旧的hk2依赖项来自org.glassfish.jersey.core:jersey-client:2.21

Then I had similar issue with org.apache.deltaspike.core:deltaspike-core-impl:1.01 . 然后我对org.apache.deltaspike.core:deltaspike-core-impl:1.01有类似的问题。 With this there was another problem - namely it was used by the cdi-unit framework in one test method. 与此相关的还有另一个问题-即cdi-unit框架在一种测试方法中使用了它。 Figuring that cdi unit and arquillian do the same thing, but the later is much more powerful I finished by excluding that test method. 弄清楚cdi单元和arquillian可以做同样的事情,但是后来我通过排除该测试方法来完成了更强大的工作。

This is the pom.xml that worked for me (I excluded the non relevant parts), but if you're having a similar issue try to follow the same logic excluding the older dependencies and "clean installing" each time to check that nothing else breaks :-): 这是为我工作的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>   
...

Note also that I have updated the java files (in the question) to improve the logical part of the tests. 还要注意,我已经更新了Java文件(在问题中)以改善测试的逻辑部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM