首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-test-dbunit找不到@Test注释

spring-test-dbunit找不到@Test注释
EN

Stack Overflow用户
提问于 2018-02-27 12:36:35
回答 1查看 747关注 0票数 0

我已经设置了spring-test-dbunit,但是我得到了以下例外:

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

测试类如下所示:

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
    ...    
    <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看起来像这样

代码语言:javascript
复制
...
<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注释无法解决。我也不知道原因

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-27 12:56:50

您的spring-test-dbunit版本很可能与春季版本不兼容。版本1.3.0中的spring-test-dbunit (似乎是最近的版本)依赖于Spring4.2.5。您可能在您的项目中使用的是最近的Spring版本,该版本在findAnnotation中不再有AnnotationUtils方法。

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

  1. 使用Spring 4(在将来放弃支持时,这可能会有问题)
  2. 找一个替代spring-test-dbunit的方法
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49008879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档