我有一个使用Maven构建的库,它使用Spring 4.0.3.RELEASE和Togglz 2.2.0.Final。我正在尝试编写我的Spring类的JUnit 4.11测试,并在第一个执行的测试中遇到以下错误:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
@PrepareForTest({ ItemServiceImpl.class })
public class ItemServiceImplTest {
@Rule
public TogglzRule togglzRule = TogglzRule.allDisabled(Features.class);
@Rule
public PowerMockRule powerMockRule = new PowerMockRule();
@Test(expected = ItemServiceBusinessException.class)
public void testCreateItem_throwsItemServiceBusinessException() throws Exception {
PowerMockito.doReturn(mockMetricsData).when(serviceUnderTest, START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));
when(mockDao.createItem(any(Item.class), any(RequestContext.class))).thenThrow(dataBusinessException);
serviceUnderTest.createItem(item, context);
verify(mockItemServiceValidator).validate(any(Item.class), any(RequestContext.class));
PowerMockito.verifyPrivate(serviceUnderTest).invoke(START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));
verify(mockDao).createItem(any(Item.class), any(RequestContext.class));
}
}
以下是相关的java测试代码段:
java.lang.NoClassDefFoundError: Could not initialize class org.togglz.junit.TogglzRule
后续测试调用会出现以下错误:
org.mockito:mockito-all=org.mockito:mockito-all:jar:1.9.5:compile,
org.powermock:powermock-module-junit4=org.powermock:powermock-module-junit4:jar:1.5.6:test,org.powermock:powermock-module-junit4-common=org.powermock:powermock-module-junit4-common:jar:1.5.6:test,
org.powermock:powermock-reflect=org.powermock:powermock-reflect:jar:1.5.6:test,
org.powermock:powermock-api-mockito=org.powermock:powermock-api-mockito:jar:1.5.6:test,
org.powermock:powermock-api-support=org.powermock:powermock-api-support:jar:1.5.6:test,
org.powermock:powermock-module-junit4-rule=org.powermock:powermock-module-junit4-rule:jar:1.5.6:test,
org.powermock:powermock-classloading-base=org.powermock:powermock-classloading-base:jar:1.5.6:test,
org.powermock:powermock-core=org.powermock:powermock-core:jar:1.5.6:test,
org.powermock:powermock-classloading-xstream=org.powermock:powermock-classloading-xstream:jar:1.5.6:test,
org.togglz:togglz-core=org.togglz:togglz-core:jar:2.2.0.Final:compile,
org.togglz:togglz-slf4j=org.togglz:togglz-slf4j:jar:2.2.0.Final:compile,
org.togglz:togglz-spring-core=org.togglz:togglz-spring-core:jar:2.2.0.Final:compile,
org.togglz:togglz-testing=org.togglz:togglz-testing:jar:2.2.0.Final:test,
org.togglz:togglz-junit=org.togglz:togglz-junit:jar:2.2.0.Final:test
以下是我的一些相关依赖项:
org.togglz.slf4j.Slf4jLogProvider
我通过SPI提供了一个LogProvider(META-INF/serivces/org.togglz.core.spi.LogProvider
),位于import numpy as np
array=np.loadtxt('filename.txt')
#multiply first column by 2
array[:,0]=array[:,0]*2
savetxt('filename2.txt',array)
这个错误令人困惑,因为Slf4jLogProvider应该可以从LogProvider分配。很抱歉,但我想尝试展示完整的图片。 “测试中”类中的代码正在调用以查看是否在create方法中启用了单个功能。
答案 0 :(得分:-1)
首先:您不需要在应用程序中配置日志提供程序。在应用程序路径中包含togglz-slf4j
就足够了,因为此jar包含相应的SPI文件。
请问您的类路径上是否存在Togglz JAR文件的多个冲突版本?例如,将togglz-core-2.2.0.Final.jar
与togglz-slf4j-2.1.0.Final.jar
一起使用可能会导致此类错误。
如果您更新Togglz并且您的IDE没有删除旧档案,则会发生这种情况。运行干净的构建和/或选择"更新Maven配置"在Eclipse上将解决这个问题。