【发布时间】: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