我正试图将mockito添加到我的arquillian测试中(使用ShrinkWrap),如下所示:
@Deployment
public static Archive<?> createDeployment() {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar")
.addPackage(BeanClass.class.getPackage())
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
;
Archive[] libs = Maven.resolver()
.loadPomFromFile("pom.xml")
.resolve(
"org.mockito:mockito-all"
)
.withTransitivity()
.as(JavaArchive.class);
for (Archive lib : libs) {
archive = archive.merge(lib);
}
return archive;
}
我正在使用Mockito用@Alternative
进行覆盖。但是当我添加行archive = archive.merge(lib)
时,我得到了异常:
引起原因:java.lang.ClassNotFoundException:org.apache.tools.ant.Task
或者我会得到
引起原因:java.lang.ClassNotFoundException:org.mockito.asm.signature.SignatureVisitor
其他人也经历过这种情况吗?
更新:一些额外的信息,我正试图用一个wildfly嵌入式容器来测试这一点:pom.xml
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-embedded</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-embedded</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-transaction-jta</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我终于找到了我需要的解决方案。我找到了一个包含蚂蚁依赖性的解决方案。当我需要使用其他测试库(如cucumber)时,问题就开始了。我现在正在测试EAR部署,它解决了我的问题:
@Deployment
public static Archive<?> createDeployment() {
final JavaArchive ejbJar = ShrinkWrap
.create(JavaArchive.class, "ejb-jar.jar")
.addClass(NewSessionBean.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
final WebArchive testWar = ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(NewSessionBeanTest.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
;
Archive[] libsArchives = Maven.resolver()
.loadPomFromFile("pom.xml")
.resolve("org.mockito:mockito-all")
.withTransitivity()
.as(JavaArchive.class);
testWar.addAsLibraries(libsArchives);
final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class)
.setApplicationXML("META-INF/test-application.xml")
.addAsModule(ejbJar)
.addAsModule(testWar);
return ear;
}
还有我的test-application.xml
<application>
<display-name>ear</display-name>
<module>
<ejb>ejb-jar.jar</ejb>
</module>
<module>
<web>
<web-uri>test.war</web-uri>
<context-root>/test</context-root>
</web>
</module>
</application>
不能将Mockito与statics或韵母一起使用。
您需要使用PowerMock才能做到这一点。