Reputation: 383
I have a service that uses Groovy SQL to make some sql calls to the database. My tests for this service are failing because dataSource is null. How do I get dataSource to be the appropriate connection inside a unit test?
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()
}
}
In the above example I get the following error:
java.lang.NullPointerException: Must specify a non-null Connection
Upvotes: 0
Views: 1635
Reputation: 75671
There's no database in unit tests, only in integration tests. Everything in unit tests uses mocking since there's no active Spring, Hibernate, database, plugins, etc. Persistence should always be tested against a database, even if it's just the in-memory HSQL or H2 database, or a dev version of a MySQL/Postgres/Oracle/etc. database.
Upvotes: 5