我尝试使用单元测试,但找不到default schema
在此示例应用程序中设置的方法。这是一个具有和的简单micronaut-data
示例。应用程序中使用 HSQL 数据库,Hikari db-poolmodel
repository
service
micronaut-jdbc-hikari
型号:Student.java
@Entity
@Table(name="student")
class Student{
@Id
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
}
存储库:StudentRepository.java
@Repository()
public interface StudentRepository extends JpaRepository<Student, Long> {
}
服务:StudentService.java
@Singleton
public class StudentService {
@Inject
StudentRepository studentRepository;
public Student getStudentById(Long id) {
Optional<Student> optional = StudentRepository.findById(id);
if (optional.isPresent()) {
return optional.get();
}
throw new ApplicationException("No matching data available");
}
}
单元测试:StudentServiceTest.java
@MicronautTest
public class StudentServiceTest {
@RegisterExtension
static InMemoryDbExtension inMemoryDbExtension = new InMemoryDbExtension.InMemoryDbBuilder()
.withSchemaName("TEST_SCHEMA")
.withSchemaSqls(new String[]{"src/test/resources/memdb/schema.sql"})
.withTestDataSqls(new String[]{"src/test/resources/db/test.sql"})
.build();
private static InMemoryHsqlDb inMemoryHsqlDb;
@Inject
private StudentService studentService;
@BeforeAll
public static void runBeforeAll(InMemoryHsqlDb inMemoryHsqlDb) {
StudentServiceTest.inMemoryHsqlDb = inMemoryHsqlDb;
}
@Test
public void shouldTestGetById() {
Student s = new Student();
s.setId(5);
s.setName("XYZ");
studentService.save(s); // query generated: insert into student(id, name) values (?,?)
}
}
问题: 有一些方法我可以找到我们可以设置默认模式的地方,但每个方法都有它的问题。
- 使用存储库注释
@Table(schema="TEST_SCHEMA", name="student")
<= Works。但不能硬编码模式名称 - 使用存储库注释
@Table(schema="#{property.file.schema_name}", name="student")
<= Spring 表达式,SpEL 不起作用
3.使用application-test.yml
文件:[没有效果,查询不选择模式名称]。
jpa:
default:
entity-scan:
packages: 'com.example.model'
properties:
hibernate:
hbm2ddl:
auto: update
default-schema: TEST_SCHEMA # <= default schema, also tried default_schema
show_sql: true
4.application-test.yml
在数据源中使用文件指定模式:但它不起作用,因为模式尚未创建,它将在测试类中创建。所以应用程序不会加载。
datasources:
default:
driverClass: org.hsqldb.jdbc.JDBCDriver
url: jdbc:hsqldb:mem:testdb;sql.syntax_ora=true
username: sa
autoCommit: false
maximumPoolSize: 1
leakDetectionThreshold: 180000
poolName: hikariConnectionPoolName
minimumIdle: 5
schema: TEST_SCHEMA # <= default schema
==================================================== ======= 暂时
的解决方法
使用同义词所以,当table_name
创建schema_name.table_name
没有模式的查询时,同义词将被替换。生成同义词,student
以便test_schema.student
正确翻译查询:
生成的查询 =>insert into student(id, name) values (?,?)
翻译的查询 =>insert into test_schema.student(id, name) values (?,?)
单元测试:StudentServiceTest.java
@BeforeAll
public static void runBeforeAll(InMemoryHsqlDb inMemoryHsqlDb) {
StudentServiceTest.inMemoryHsqlDb = inMemoryHsqlDb;
// workaround for not using schema
inMemoryHsqlDb.getJdbi().open().execute("create synonym STUDENT for TEST_SCHEMA.STUDENT");
}