JDBC + DBUnit 未找到测试
JDBC + DBUnit no tests found
我正在尝试为我的 DAOImpl
class 编写测试,只需插入一个查询
但是测试不工作,给出这样的错误:junit.framework.AssertionFailedError: No tests found in ...
找不到有关此问题的任何信息。
测试class:
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);
}
}
DBTestCase 是 JUnit 4 class 并且您正在使用 JUnit 5 中的 @Test。需要决定使用哪个版本并相应地更新。
此外,不要扩展 dbUnit 测试 class,而是使用组合。
我正在尝试为我的 DAOImpl
class 编写测试,只需插入一个查询
但是测试不工作,给出这样的错误:junit.framework.AssertionFailedError: No tests found in ...
找不到有关此问题的任何信息。
测试class:
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);
}
}
DBTestCase 是 JUnit 4 class 并且您正在使用 JUnit 5 中的 @Test。需要决定使用哪个版本并相应地更新。
此外,不要扩展 dbUnit 测试 class,而是使用组合。