JDBC + DBUnit未找到测试

问题描述 投票:0回答:1

[我正在尝试为我的DAOImpl类编写测试,只是简单地插入一个查询但是测试无法正常工作,并显示以下错误:junit.framework.AssertionFailedError: No tests found in ...

无法找到有关此问题的任何信息。

测试类:

import org.dbunit.Assertion;
import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.junit.jupiter.api.Test;
import test.database.dao.DaoFactory;
import test.database.dao.interfaces.CoursesDao;
import test.models.Course;

import java.io.File;

import static org.junit.jupiter.api.Assertions.*;

public class CoursesDaoImplTest extends DBTestCase {

public CoursesDaoImplTest(String name) {
    super(name);
    System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.postgresql.Driver");
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:postgresql://localhost:5432/school" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "school_admin" );
    System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "admin" );
}

@Override
protected IDataSet getDataSet() throws Exception {
    return new FlatXmlDataSetBuilder().build(new File(getClass().getClassLoader()
            .getResource("/school-data.xml").getFile()));
}

@Test
public void TestAdd_ShouldAddCourse_WhenInputNewCourse() throws Exception {

    Course course = new Course("Archery", "Description");

    CoursesDao coursesDao = DaoFactory.getCoursesDao();
    coursesDao.add(course);

    IDataSet databaseDataSet = getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable("courses");

    IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(getClass().getClassLoader()
            .getResource("/coursesDaoImplTest-add-expected.xml").getFile()));
    ITable expectedTable = expectedDataSet.getTable("courses");

    Assertion.assertEquals(expectedTable, actualTable);
}

}

java testing jdbc dbunit
1个回答
0
投票

DBTestCase是一个JUnit 4类,您正在使用JUnit 5中的@Test。需要确定要使用的版本并进行相应的更新。

此外,请使用composition,而不是扩展dbUnit测试类。

© www.soinside.com 2019 - 2024. All rights reserved.