MicronautTest Datasource setup not clean before each execution - spock

I have this micronaut application doing some basic database lookup, just for testing the framework. I have setup the application with flyway database migrations, running a h2 database in my test setup (production uses postgresql). This is used for setting up the schema correctly.
My repository is created using Micronaut data for jpa, so I do not know how to create it without running the tests in full micronaut context.
In my test I try to insert some data in the setup method, before executing some spock tests. When the setup has run once, I would expect it to start with a clean datasource for the next test execution. Since the data will violate a unique constraint, it fails with an sql error in setup for the second test being run. Here's my code:
#MicronautTest()
class CompanyRepositoryTest extends Specification {
#Inject
CompanyRepository repository
#Inject
DataSource dataSource
def sql
def setup() {
sql = new Sql(dataSource: dataSource)
sql.execute(dataSql)
}
def 'test company creation' () {
given:
def name = 'test2'
def orgNumber = '1232429045'
when:
def company = repository.save(new Company(name: name, organizationNumber: orgNumber))
then:
company.id != null
}
#Unroll
def 'get company for #desc'() {
when:
Company result = repository.find(id)
then:
name == null ? result == null : name == result.name
where:
id | name | desc
1 | 'test1' | 'existing company'
2 | null | 'non-existing company'
}
def dataSql = """
insert into company(name, organization_number) values
('test1', '1232429045');
"""
I guess I could create a cleanup block where I execute delete statements, but I see that as a last resort as I would expect the datasource to be clean before each test run.

After a good nights sleep, the obvious answer came to me. Instead of executing the sql through groovy sql,I use entitymanager. Then this works as expected
#Inject
EntityManager entityManager
def setup() {
entityManager.createNativeQuery(dataSql).executeUpdate();
}

Related

Grails integration test mock not cleaned

I've a strange polluting situation between two spock integration tests that I'm not able to resolve. I suppose that I'm doing something wrong but I can't understand what.
The two integration tests are testing different situation of the same controller. In the first one I mock a service while in the second I don't mock.
Here are the significant parts of two tests :
test 1:
// CodeControllerSpec.groovy
...
def controller = new CodeController()
def serviceMock = new MockFor(PollutingService)
serviceMock.demand.search(1) { a, b, c ->
return [id: 1]
}
controller.myService.pollutingService = serviceMock.proxyInstance()
controller.save()
...
then:
serviceMock.verify(controller.myService.pollutingService)
test 2:
// CodeEngineSpec.groovy
...
def controller = new CodeController()
controller.show()
...
then:
...
Controller and services are as following
// CodeController
class CodeController extends RestfulController<Code> {
def myService
def show() {
...
myService.execute()
...
}
}
// MyService
class MyService {
def pollutingService
def execute() {
...
pollutingService.search(a, b, c)
...
}
}
// PollutingService
class PollutingService {
def search(a, b, c) {
...
...
}
}
If I run the two tests one by one, they all pass but, if I run them together, the second one fails with
No more calls to 'search' expected at this point. End of demands.
I'm sure that the mock in the first service is used (I've debugged code line by line) but I don't know why mock is not cleaned after test.
Any suggestion is really welcome.
I'm using grails 2.3.8
First of all, using mocks in integration tests has unpredictable results.
But putting that aside, where is controller.myService in your first test being instantiated? I would have expected that calling controller = new CodeController() would bypass autowiring of controller.myService.

Grails spock service test not calling service method

I'm seeing some strange behavior in a spock test for a service using Grails 2.3.7.
This test works fine:
void "create spot order"() {
given:
def createOrderCommand = newCreateOrderCommand(OrderType.S)
when:
def orderId = service.createOrder(createOrderCommand, user).id.toInteger()
then:
Order.count() == 1
when:
def order = service.orderById(orderId)
then:
// a bunch of assertions
}
This test also works fine:
void "create command with invalid order id"() {
when:
service.commandForOrderId(999)
then:
def exception = thrown(CreateOrderException)
exception.key == "orderService.invalid.order.id"
}
However this test fails - I've set a breakpoint at the beginning commandForOrderId, and it is never hit. command is null (this is where the test fails, on the line checking for null on command), which would never be returned from this service method:
void "create spot command"() {
given:
def createOrderCommand = newCreateOrderCommand(OrderType.S)
when:
def order = service.createOrder(createOrderCommand, user)
then:
Order.count() == 1
when:
def orderId = order.id.toInteger()
def command = service.commandForOrderId(orderId)
then:
command
// a bunch more assertions
}
I've tried removing the #Transactional annotation from the service, using the transactional static field, as well as using neither of these. I've also tried changing the service method to a closure, all with no luck.
I changed the service method to a closure, cleared my target directories, did every manner of clean I know of (within GGTS and grails commands), and the service method started getting hit in the test. I'm still unsure of the actual cause of this error, however.

What is wrong with my Spec?

So, I have this pretty simple Spec below. I have a class that is not a controller or service or anything like that. It's a Job class. It depends on two services: updateService and directoryTypeService. It runs a Redis async job and it's under /grails-app/jobs folder.
All I want is to make sure that whenever I invoke this job#perform() method (which return type is void), a given dependent method called UpdateService#completeClaiming is invoked, but UpdateService#requestNewPin is not. (Listing is a domain class, by the way).
When I run this Spec, I keep getting an error message saying: "No more calls to 'completeClaiming' expected at this point. End of demands."
What am I doing wrong here? Any wild guesses?
#Mock(Listing)
class SubmissionJobSpec extends Specification {
def directoryTypeServiceMock
def updateServiceMock
def job
def setup(){
job = new SubmissionJob()
directoryTypeServiceMock = mockFor(DirectoryTypeService)
updateServiceMock = mockFor(UpdateService)
job.updateService = updateServiceMock.createMock()
job.directoryTypeService = directoryTypeServiceMock.createMock()
}
def "if the directory is enabled and the pin status is ENTERED, we should call updateService.completeClaiming"() {
given:
directoryTypeServiceMock.demand.isUpdateEnabled { DirectoryType d, Country c -> return true}
new Listing(
location: new Location(country: Country.DE)
).save(failOnError: true, validate: false)
when:
job.perform(Listing.last().id, true)
then:
1 * updateServiceMock.completeClaiming(Listing.last(), true) >> new ListingEvent(output: [success: true])
0 * updateServiceMock.requestNewPin(_ as Listing, true)
}
You seem to be confusing Groovy and Spock mocks. You can't use Spock mocking syntax (e.g. 0 * updateServiceMock.requestNewPin(_ as Listing, true)) for a Groovy mock created with mockFor(). Spock mocks are created using Mock(), Stub() or Spy(). I'm not aware of any good reason to use a Groovy mock in a Spock spec.

How to avoid null field errors in spock for Grails domain mock

Using grails 2.2.0 given this simple Domain
class Order {
static mapping = {table "ticket_order"}
String foo
}
And associated spock test
#TestFor(Order)
class OrderSpec extends Specification {
def "sensible constraints for order class"() {
setup:
mockForConstraintsTests(Order)
when:
def order = new Order(
foo : foo
)
order.validate()
then:
!order.errors.hasFieldErrors("foo")
where:
foo = "bar"
}
}
I get this output
grails> test-app unit: Order -echoOut
| Running 1 spock test... 1 of 1
--Output from sensible constraints for order class--
| Failure: sensible constraints for order class(uk.co.seoss.presscm.OrderSpec)
| Condition not satisfied:
!order.errors.hasFieldErrors("foo")
|| | |
|| | true
|| org.codehaus.groovy.grails.plugins.testing.GrailsMockErrors: 1 errors
|| Field error in object 'uk.co.seoss.presscm.Order' on field 'foo': rejected value [null];
Could someone please explain why I'm getting that null, have I not set the property correctly? I've tried a few simpler formulations of this with no joy. It works fine in a standard unit test.
It looks to me like your mixing the data-driven and interaction based testing styles.
The where block is mentioned only in the context of data-driven, and the when/then combo in the context of interaction testing.
Try put def foo = "bar" at the top of the test.

Integration Tests started to fail with grails upgrade

I upgraded a grails app from 1.2.2 to 1.3.7 after this upgrade a few integration tests have started to throw the following error the 'validateAndSaveList' is a method on a service used by the service I'm testing. These tests were passing before the upgrade and they will also pass if I run just the integration test phase with grails test-app -integration
junit.framework.AssertionFailedError:
No more calls to 'validateAndSaveList'
expected at this point. End of
demands.
Code:
import com.e.domain.*
import com.e.exception.GORMServiceException
import com.e.controller.SecurityUserCommand
class AccountServiceTests extends GroovyTestCase
{
def accountService
void testRegisterWithMinimumInfo()
{
def clinic = new Clinic(name:'clinicName')
def securityUserCommand = new SecurityUserCommand(username:'username', password:"password", confirm:"password")
def clinicUser = new ClinicUser(firstName:'fname', lastName:'lname', emailAddress:'abc#abc.com')
clinicUser.clinic = clinic
//clinicUser.securityUser = securityUser
clinic.address = new Address()
// TODO - JsecUser no longer in use
def role = new ShiroRole(name:'TEST')
//def subscription = ESubscription.findByName('Charter Member')
def subscription = new ESubscription(
name:'Charter Member',
description:'Charter Member',
periodType:'Monthly',
numPeriods:12,
amountPerPeriod:25.00,
electronicSubmissionRate:0.00,
accountingRate:0.01,
numAllowedUsers:4,
startDate: today -1,
endDate: today+1
)
subscription.save(flush:true)
if(subscription.hasErrors())
println subscription.errors
assertNotNull subscription
clinicUser.empathicCustomerProfile.subscription = subscription
def result = accountService.register(clinic, securityUserCommand, clinicUser, role)
assert result.success
assert result.clinic.id
assert result.securityUser?.id
assert result.clinicUser.id
}
StackTrace
junit.framework.AssertionFailedError: No more calls to 'validateAndSaveList' expected at this point. End of demands.
at grails.test.MockClosureProxy.doBeforeCall(MockClosureProxy.java:66)
at grails.test.AbstractClosureProxy.call(AbstractClosureProxy.java:74)
at grails.test.GrailsMock$_createMock_closure1.doCall(GrailsMock.groovy:125)
at com.e.service.AccountService.register(AccountService.groovy:46)
at com.e.service.AccountService$register.call(Unknown Source)
at AccountServiceTests.testRegisterWithMinimumInfo(AccountServiceTests.groovy:53)
this answer comes from working the issue out in the comments:
the exception you are getting clearly indicates that somewhere you have put a mock object in your service, and the service is calling the mock object in a way it was not set up to handle.
The root problem as seen from #hvgotcodes is that there was a mock object for the service even though in that given test there was no mocking happening.
This happened in grails 1.3.7
I found a unit test that was doing the following:
def dataBindServiceControl = mockFor(DataBindService)
dataBindServiceControl.demand.safeBind{}
dataBindServiceControl.demand.extractPhones{}
dataBindServiceControl.demand.validateAndSaveList{l-> return true}
def dataBindService = dataBindServiceControl.createMock()
controller.dataBindService = dataBindService
If those tests were removed then all the integration tests would pass so to solve with out rewriting the tests I added the following to the tear down method.
GroovySystem.metaClassRegistry.removeMetaClass(DataBindService)
With this addition the tests are now working correctly in grails 1.3.7

Resources