0

我已经设置了 spring-test-dbunit 但我得到了以下异常:

testSometing(com.my.package.dbunit.DbUnit) 经过时间:13.013 秒 <<< 错误!java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.findAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

测试类如下所示:

package com.my.package.dbunit;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-application.xml")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class, DbUnitTestExecutionListener.class})
public class DbUnit {

    @Autowired
    public MyDAO myDAO;

    @Test
    @DatabaseSetup("target/partial.xml")
    public void testSometing() throws Exception {
        int rootId = 123;
        MyClass root = myDAO.getById(rootId);
    }
}

test-application.xml 如下所示:

    ...    
    <bean id="dataSource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    ...

pom.xml 看起来像这样

...
<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.5.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.github.springtestdbunit</groupId>
    <artifactId>spring-test-dbunit</artifactId>
    <version>1.3.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
...

似乎存在错误,因为@Test无法解析注释。我不知道为什么

4

1 回答 1

3

您的版本spring-test-dbunit很可能与您的 spring 版本不兼容。spring-test-dbunit在 1.3.0 版本(似乎是最新版本)中取决于 Spring 4.2.5。您可能在项目中使用了更新的 Spring 版本,该版本不再有findAnnotation方法AnnotationUtils

您现在基本上可以做两件事:

  1. 使用 Spring 4(将来可能会出现问题,当支持被删除时)
  2. 寻找替代方案spring-test-dbunit
于 2018-02-27T12:56:50.903 回答