一些可能有用的信息:
这是正在测试的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&#39;设置上面提到的maven配置文件。 &#39;设置配置&#39;选项。
这是我试图运行的测试(非常基本,如您所见):
@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)
...
答案 0 :(得分:2)
最后我明白了。我发布它,以防这对其他遇到类似问题的人有用。所有问题都来自冲突的依赖关系,但其中许多都是看不见的,因为我使用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文件(在问题中)以改进测试的逻辑部分。