简体   繁体   中英

spring-test-dbunit can't find @Test annotation

I've set up spring-test-dbunit but I get following exception:

testSometing(com.my.package.dbunit.DbUnit) Time elapsed: 13.013 s <<< ERROR! java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.findAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

The test class looks like following:

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);
    }
}

The test-application.xml looks like following:

    ...    
    <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 looks like this

...
<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>
...

It seems like that the error exists because the @Test annotation can't be resolved. I don't know why

Your version of spring-test-dbunit is most probably incompatible with your spring-version. spring-test-dbunit in version 1.3.0 (which seems to be the most recent one) depends on Spring 4.2.5. You are probably using a more recent Spring version in your project which does not have a findAnnotation method in AnnotationUtils any more.

You basically have two things that you can do right now:

  1. Use Spring 4 (which might be problematic in the future, when support is dropped)
  2. Find an alternative to spring-test-dbunit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM