Skip to content

GSON unit test controller - failing #49

Description

@ashok84munna

I'm trying to write a unit test for a simple controller that takes GSON as input and writes to DB.
GSON because, the input has nested objects.

Controller implementation:

def create() {
    def artist = new Artist(request.GSON)
    if (!artist.save(flush: true)) {
        artist.errors.each {
            log.error("Error while creating Artist " + it)
        }
        render status: 500, layout: null
        return
    }
    response.status = 201
    render artist as GSON
}

Unit test:

@TestMixin(GsonUnitTestMixin)
@TestFor(ArtistController)
@Mock(Artist)
class ArtistControllerTests {
    void testCreate() {
        request.GSON = "{guid:123,"+
                                           "name: 'Akon',"+
                    "albums: ["+
                    "{guid:1,"+
                    "name:'album 1'"+
                    "}]}"
       controller.create()
       assert response.status == 201
      }
   }

Exception:
Cannot get property 'manyToOne' on null object at
def artist = new Artist(request.GSON) in the controller

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions