【问题标题】:JUnit + DbUnit - No test found when extending DatabaseTestCaseJUnit + DbUnit - 扩展 DatabaseTestCase 时未找到测试
【发布时间】:2016-07-16 20:29:11
【问题描述】:

我最近开始使用 DbUnit,我正在尝试编写一个非常简单的集成测试来填充一个包含 3 行的表。阅读 DbUnit Getting Started Guide,它告诉我创建一个数据集文件。我的数据集 xml 文件看起来完全像这样:

<dataset>
    <notaFiscal cliente="Cliente 1" valor="26.5" data='2016-04-04'/>
    <notaFiscal cliente="Cliente 2" valor="30.5" data='2016-05-01'/>
    <notaFiscal cliente="Cliente 3" valor="28.2" data='2015-08-11'/>
</dataset>

然后,我必须创建一个扩展 DBTestCase 的测试类并实现我的测试方法(使用 @Test 注释,就像任何其他 JUnit 测试用例一样)。我创建的类如下:

public class GerenciadorNFTest extends DBTestCase {

    private GerenciadorNotaFiscal gerenciador = new GerenciadorNotaFiscal();

    public GerenciadorNFTest(String name)
    {
        super( name );
        // PBJDT is an abbreviation of PropertiesBasedJdbcDatabaseTester
        // just for a better visualization
        System.setProperty(PBJDT.DBUNIT_DRIVER_CLASS, 
            "org.postgresql.Driver" );
        System.setProperty(PBJDT.DBUNIT_CONNECTION_URL, 
            "jdbc:postgresql://localhost:5432/dbunit" );
        System.setProperty(PBJDT.DBUNIT_USERNAME, "postgres" );
        System.setProperty(PBJDT.DBUNIT_PASSWORD, "123456" );
    }


    protected IDataSet getDataSet() throws Exception {
        IDataSet dataSet = new FlatXmlDataSetBuilder().build(
            new FileInputStream("notas_fiscais.xml"));
        return dataSet;
    }

    @Test
    public void geraPedido() {
        Pedido p = new Pedido("Diogo", 26d, 5);
        gerenciador.gera(p);
        NotaFiscal notaFiscal = gerenciador.recupera("Diogo");
        Assert.assertEquals(notaFiscal.getCliente(), "Diogo");
    }

}

之后,我尝试运行测试用例,但出现以下错误:

junit.framework.AssertionFailedError: No tests found in teste.GerenciadorNFTest

    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.TestCase.fail(TestCase.java:227)

如果我尝试删除extend DBTestCase,JUnit 会识别测试用例并正常运行,但扩展它没有。我试图清理并重新编译,但它没有工作。我还尝试在我使用的 IDE(Intellij Idea)之外运行测试,但我还是没有成功。

有人遇到过同样的问题吗? 非常感谢您。任何帮助将不胜感激。

【问题讨论】:

    标签: java junit integration-testing dbunit


    【解决方案1】:

    JUnit 3 vs 4 runner 的差异可能是原因(您没有提到 JUnit 和 dbUnit 版本,也没有提到依赖项如何管理它们)。并且不同的工具有不同的运行默认要求(例如 Maven 默认只运行类作为测试,类名后缀为“Test”)。

    请注意,不需要扩展 dbUnit 类(我不需要),不这样做应该可以消除遇到的问题。在您提到的页面下方有两个部分描述了如何:

    将两者结合起来是我多年来一直在做的事情 - 拥有自己的父测试类来处理常见的东西,然后 DI(或实例化)所需的 DBTestCase(通常是 PrepAndExpectedTestCase)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2020-03-25
      • 2013-12-02
      • 2011-05-14
      相关资源
      最近更新 更多