我有一个服务,使用Groovy SQL对数据库进行一些SQL调用.我对此服务的测试失败,因为dataSource为null.如何让dataSource成为单元测试中的适当连接?
ExampleService.groovy
class ExampleService {
def dataSource
def getSQL() {
def sql = new Sql(dataSource)
def query = "some query"
sql.call(query)
sql.close()
}
}
Run Code Online (Sandbox Code Playgroud)
ExampleServiceTests.groovy
@TestFor(ExampleService)
class ExampleServiceTests {
void testExample() {
def es = new ExampleService()
es.getSQL()
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我收到以下错误:
java.lang.NullPointerException: Must specify a non-null Connection
Run Code Online (Sandbox Code Playgroud)
单元测试中没有数据库,仅在集成测试中.单元测试中的所有内容都使用模拟,因为没有活动的Spring,Hibernate,数据库,插件等.持久性应始终针对数据库进行测试,即使它只是内存中的HSQL或H2数据库,也不是MySQL的开发版本/ Postgres的/甲骨文/等.数据库.