简体   繁体   中英

JUnit + DbUnit - No test found when extending DatabaseTestCase

I'm getting started with DbUnit lately and I'm trying to write a very simple integration test just to populate a table with 3 rows. Reading the DbUnit Getting Started Guide , it tells me to create a dataset file. My dataset xml file looks exactly like this:

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

Then, I have to create a test class which extends DBTestCase and implement my test methods (annotated with @Test , like any other JUnit test case). The class I created is as follows:

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

}

After that, I tried to run the test case but got the following error:

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)

If I tried to remove the extend DBTestCase , JUnit recognize the test case and runs normally, but with the extend it didn't. I tried to clean and re-compile, but it didn't work. I also tried to run the test outside the IDE I use (Intellij Idea) but again I had no sucess.

Has anyone been through this same problem? Thank you very much in advance. Any help will be appreciated.

There are JUnit 3 vs 4 runner differences that may be the cause (you don't mention JUnit and dbUnit versions, nor how dependency managing them). And different tooling has different running default requirements (eg Maven defaults to only running classes as tests with class name suffix of "Test").

Note that it is not required to extend a dbUnit class (I don't) and not doing so should eliminate the encountered problem. Just further down that page you mentioned are two sections describing how:

And combining both is what I've done for years - have my own parent test class for common stuff and then DI (or instantiate) the desired DBTestCase (usually PrepAndExpectedTestCase).

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