1
votes

I'm trying to test a controller for a subclass in Grails 2.4.3. domain model is like this:

abstract class Task { ... }

class SubTask extends Task { ... }

When trying to run generated tests for class SubTaskController, 3 of 7 tests fail:

  1. save
  2. update
  3. delete

It seems that (at least) the id of the domain class is not initialized, though I defined this in the populateValidParams() method. The debugger shows two (!) generated id attributes on the subTaskInstance object, both null. When I set the id's to some long value in the debugger, the error at least comes a few lines later. Is there a bug in mocking subclasses or am I doing something fundamentally wrong?

I also tried it without "abstract" in the base class, but got same errors. The generated tests work fine for objects not in an inheritance hierarchy.

When I additionally set the id attribute directly in the test code, tests fail at the SubTask.count() == 1 assertion. Seems that the persistence layer mock does not handle subclasses.

1
Any chance you could show us the code? - Dónal
I expect that populateValidParams is not adding necessary request parameters. See the project linked in my answer below. - Jeff Scott Brown
Thank you, Jeff, you brought me back to the right way ;). I forgot to initialize a property with the size constraint 0..2000 . Not quite obvious you have to initialize this, but anyhow ... - Geobe
@Geobe Excellent. I am glad that you got it worked out. - Jeff Scott Brown
I am not sure why this question currently has a close vote for "Unclear what you're asking". 99% of the time when those 3 tests in particular fail, it is because the test hasn't property populated the request parameters, either because they aren't populated at all or because they are populated with values that are not consistent with constraints. - Jeff Scott Brown

1 Answers

0
votes

See the project at https://github.com/jeffbrown/controllerinheritancetest. Both of the controller unit tests there pass.

Are you sure that you have setup valid data in your populateValidParams method in the controller unit test? That is a likely cause of your failing tests.