运行Junit @Test方法时出现NoSuchMethodError

时间:2016-03-26 09:26:00

标签: spring hibernate spring-mvc junit

我有一个应用程序(使用注释的Spring 4 MVC + Hibernate 4 + MySQL + Maven集成示例),使用基于注释的配置将Spring与Hibernate集成。

我有这个Junit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class, BasicDataSourceHibernateConfig.class },
                      loader = AnnotationConfigContextLoader.class)
@Transactional(transactionManager = "transactionManager")
public class DeviceEventServiceImplTest {


    @Inject
    DeviceEventService deviceEventService;



    @Test
    public void testFindWithActiveAlarm() throws Exception {
        List<DeviceEvent> deviceEvents = getDeviceEventService().findWithActiveAlarm();
        System.out.println(deviceEvents);
    }



    public DeviceEventService getDeviceEventService() {
        return deviceEventService;
    }


    public void setDeviceEventService(DeviceEventService deviceEventService) {
        this.deviceEventService = deviceEventService;
    }
}

但是当我运行它时出现这个错误:

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;
    at org.springframework.test.util.MetaAnnotationUtils$AnnotationDescriptor.<init>(MetaAnnotationUtils.java:290)
    at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:365)
    at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:360)
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:191)
    at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:166)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:274)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:110)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:154)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:145)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

3 个答案:

答案 0 :(得分:1)

java.lang.NoSuchMethodError的一个主要原因是不兼容的jar文件的多个版本。检查Effetctive pom文件,确保您只有一个与弹簧/弹簧启动版兼容的文件。在上面的例子中,它将是春天依赖的冲突。

答案 1 :(得分:0)

这是因为&#34; activemq-all&#34; jar里面有一个不同版本的Spring 您应该改为使用简化版本的activemq jar。

答案 2 :(得分:0)

不需要检查您的类路径, 是的,在大多数情况下,这是由所使用的JARS版本不同引起的。 对我来说,这与Spring Boot版本有关, 因此,下面有一些调试步骤:

1)检查您的txt调试文件(通常在目标/安全报告中) 2)在此调试文件中,获取引起错误的软件包(因为它是spring boot) 3)然后使用此软件包执行此命令,并检查所有依赖项是否在同一版本中:

mvn dependency:tree | grep spring-boot 

4)对我来说,所有版本的spring boot都是2.1.1.RELEASE,除了org.springframework.boot:spring-boot-test:jar是1.4

5)修复错误版本,使其与其他版本相同。

6)运行:

mvn -U clean package

选项-U,它强制maven检查远程存储库中的所有工件并更新本地存储库(如果过时)。