我编写了一个使用Mysql嵌入式数据库的集成测试。当我在IDE上执行测试类时,这些测试与出现在同一类中的其他集成测试一起成功执行。当我通过命令行使用Gradle一起运行所有测试时,就会出现问题。
通过命令行运行时出现的错误如下:
EmployeeRepositoryFindByEmployeeIdTest > shouldReturnDeparmentForEmployeeId() FAILED
java.lang.AssertionError:
Expecting:
<[Employee(ud=118 age=23,department=null),
Employee(id=219, age=24, department=Deparment(id=5, name=IT))]>
to contain exactly (and in same order):
<[Employee(ud=118 age=23,department=Deparment(id=4, name=IT)),
Employee(id=219, age=24, department=Deparment(id=5, name=IT))]>
but some elements were not found:
<[Employee(ud=118 age=23,department=Deparment(id=4, name=IT))]>
and others were not expected:
<[Employee(ud=118 age=23,department=null)]>
EmployeeRepositoryFindByEmployeeIdTest.shouldReturnDeparmentForEmployeeId(EmployeeRepositoryFindByEmployeeIdTest.java:188)
测试调用以下方法:
@Repository
public class EmployeeRepository {
private final DSLContext dsl;
public EmployeeRepository(DSLContext dsl, Clock clock) {
this.dsl = dsl;
}
// ...
public List<Employee> getEmployeeByDepartmentId(int departmentId) {
return dsl.select(
EMPLOYEE.ID,
EMPLOYEE.AGE,
EMPLOYEE.DEPARMENT_ID,
DEPARTMENT.ID,
DEPARTMENT.NAME
).from(EMPLOYEE)
.leftJoin(DEPARMENT).on(DEPARMENT.ID.eq(EMPLOYEE.DEPARMENT_ID))
.where(DEPARMENT.ID.eq(departmentId))
.orderBy(EMPLOYEE.ID)
.fetch()
.map(this::getEmployee);
}
}
调用测试方法后,期望得到两名员工,并且每个员工都关联一个部门,但是我得到的总是null
和department_id=4
。在我看来,对于表中的某些行,联接未正确执行。
我试图解决的问题是将MySQL配置中的wait_timeout
和max_connections
增加到很高,但是我仍然看到相同的问题。另外,我升级到了最新的数据库版本-Version.v5_7_latest
-,但仍然相同。我不确定要解决此问题的其他位置。关于如何前进的任何想法?
如果有帮助,这是测试设置中的MySQL配置:
@Bean
public EmbeddedMysql embeddedMySql() {
if (embeddedMysql != null) {
embeddedMysql.reloadSchema("deparment", Collections.emptyList());
return embeddedMysql;
} else {
MysqldConfig config = aMysqldConfig(Version.v5_6_36)
.withCharset(Charset.UTF8)
.withPort(3322)
.withUser("department", "*******")
.withServerVariable("wait_timeout", "60")
.withServerVariable("max_connections", "500")
.build();
embeddedMysql = anEmbeddedMysql(config)
.addSchema("department")
.start();
return embeddedMysql;
}
}
预先感谢您的帮助!