JUnit + DbUnit - 扩展 DatabaseTestCase 时未找到测试
JUnit + DbUnit - No test found when extending DatabaseTestCase
我最近开始使用 DbUnit,我正在尝试编写一个非常简单的集成测试,只是为了用 3 行填充 table。读取 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
的测试 class 并实现我的测试方法(用 @Test
注释,就像任何其他 JUnit 测试用例一样)。我创建的class如下:
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 会正常识别测试用例和 运行s,但扩展后它不会。我试图清理并重新编译,但没有用。我还尝试 运行 我使用的 IDE 之外的测试(Intellij Idea),但我还是没有成功。
有没有人遇到过同样的问题?
非常感谢你提前。任何帮助将不胜感激。
JUnit 3 与 4 运行器的差异可能是原因(您没有提到 JUnit 和 dbUnit 版本,也没有提到它们的依赖关系管理方式)。并且不同的工具有不同的 运行 默认要求(例如,Maven 默认只有 运行 classes 作为 class 名称后缀为 "Test" 的测试)。
请注意,不需要扩展 dbUnit class(我不需要),不这样做应该可以消除遇到的问题。在您提到的那个页面的下方,有两个部分描述了如何:
- 使用您自己的父级 class 或 dbUnit IDatabaseTester 实例:http://dbunit.sourceforge.net/howto.html#Database_setup_with_your_own_TestCase_subclass
- 不使用父级 class:http://dbunit.sourceforge.net/howto.html#Database_setup_with_no_parent_class
结合这两者是我多年来所做的 - 对常见的东西进行自己的父测试 class,然后 DI(或实例化)所需的 DBTestCase(通常是 PrepAndExpectedTestCase)。
我最近开始使用 DbUnit,我正在尝试编写一个非常简单的集成测试,只是为了用 3 行填充 table。读取 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
的测试 class 并实现我的测试方法(用 @Test
注释,就像任何其他 JUnit 测试用例一样)。我创建的class如下:
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 会正常识别测试用例和 运行s,但扩展后它不会。我试图清理并重新编译,但没有用。我还尝试 运行 我使用的 IDE 之外的测试(Intellij Idea),但我还是没有成功。
有没有人遇到过同样的问题? 非常感谢你提前。任何帮助将不胜感激。
JUnit 3 与 4 运行器的差异可能是原因(您没有提到 JUnit 和 dbUnit 版本,也没有提到它们的依赖关系管理方式)。并且不同的工具有不同的 运行 默认要求(例如,Maven 默认只有 运行 classes 作为 class 名称后缀为 "Test" 的测试)。
请注意,不需要扩展 dbUnit class(我不需要),不这样做应该可以消除遇到的问题。在您提到的那个页面的下方,有两个部分描述了如何:
- 使用您自己的父级 class 或 dbUnit IDatabaseTester 实例:http://dbunit.sourceforge.net/howto.html#Database_setup_with_your_own_TestCase_subclass
- 不使用父级 class:http://dbunit.sourceforge.net/howto.html#Database_setup_with_no_parent_class
结合这两者是我多年来所做的 - 对常见的东西进行自己的父测试 class,然后 DI(或实例化)所需的 DBTestCase(通常是 PrepAndExpectedTestCase)。