我们有一个使用
的项目在编写单元测试时,为了测试与本机库交互的托管bean,我们想要模拟它们所定义的类。但是,PowerMock(具体来说,注释@PrepareForTest
)似乎与Weld的初始化相混淆。例如,如果我有以下内容:
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticNativeAPI.class)
public class Test {
@Before
public void setup() {
WeldContainer wc = new Weld().initialize();
}
}
然后Weld将无法初始化,并出现以下错误:
org.jboss.weld.exceptions.DeploymentException:WELD-001423不能 启用相同的替代bean类 [com.mycompany.AltBean in 罐子:文件:/ C:/Users/Me/.m2/repository/com/mycompany/project/version-SNAPSHOT/project-version-SNAPSHOT-tests.jar /META-INF/beans.xml@8, com.mycompany.AltBean in 罐子:文件:/ C:/Users/Me/.m2/repository/com/mycompany/project/version-SNAPSHOT/project-version-SNAPSHOT-tests.jar /META-INF/beans.xml@8] 在beans.xml中
at org.jboss.weld.manager.Enabled.createMetadataMap(Enabled.java:123)
at org.jboss.weld.manager.Enabled.<init>(Enabled.java:94)
at org.jboss.weld.manager.Enabled.of(Enabled.java:79)
at org.jboss.weld.bootstrap.BeanDeployment.<init>(BeanDeployment.java:114)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:184)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:153)
at org.jboss.weld.bootstrap.WeldBootstrap.startContainer(WeldBootstrap.java:284)
at org.jboss.weld.bootstrap.api.helpers.ForwardingBootstrap.startContainer(ForwardingBootstrap.java:42)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:129)
at com.mycompany.Test.setup(Test.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:56)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:43)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
换句话说,它似乎是解析beans.xml
两次。有谁知道如何让这两个库很好地一起玩?