2012-03-30 34 views
0

我有一个服务使用Groovy SQL对数据库进行一些sql调用。我的这个服务的测试失败,因为dataSource为空。我如何获得dataSource在单元测试中成为适当的连接?使用SQL和数据源测试服务

ExampleService.groovy

class ExampleService { 
    def dataSource 

    def getSQL() { 
     def sql = new Sql(dataSource) 
     def query = "some query" 
     sql.call(query) 
     sql.close() 
    } 
} 

ExampleServiceTests.groovy

@TestFor(ExampleService) 
class ExampleServiceTests { 
    void testExample() { 
     def es = new ExampleService() 
     es.getSQL() 
    } 
} 

在上面的例子中,我得到以下错误:

java.lang.NullPointerException: Must specify a non-null Connection 
+0

def es = new ExampleService() – Churk 2012-03-30 16:18:15

+0

你是对的Churk,这是我的错误,谢谢。 – thomj 2012-03-30 17:33:56

回答

5

有没有数据库I n单元测试,仅在集成测试中。因为没有活跃的Spring,Hibernate,数据库,插件等,所以单元测试中的所有东西都使用模仿。持久性应该总是针对数据库进行测试,即使它只是内存中的HSQL或H2数据库,或者是开发版的MySQL/Postgres的/甲骨文/等。数据库。