Mocking of class is not working in Spock Test case

I am trying write a test case for Rest Post method which gets request object, converts into domain object and saves an object using spring jpa repository.

Folder Structure:

core.jar - domain, repository and daos core.war - service, spock test, controllers. core.jar is part dependency jar

Example Request:

public class RequestObject {

    private Long value2;
    private Long value1;

... getters and setters

}

Domain Object

public class DomainObject {

        private Object object1;
        private Object object2;
        private type field1;
        private type field2;
        private long version;
        private date datecreated

    ... getters and setters

    }

Service Method

  @Autowired
  DomainDAO domainDAO; // DomainDAO has domainRepository Autowired

@RequestMapping( value = "/domain", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE )
public DomainObject saveDomainObject(@RequestBody RequestObject  request)
{
   return domainDAO.saveDomain(buildDomainObject(request))
}

private DomainObject buildDomainObject(RequestObject  request)
{
  DomainObject object = new DomainObject()
  object.setField1(request.getValue2());
  etc ....
}

I am to successfully save/update if I using REST Client/SOAP/Swagger to the database. But when I try to write the spock test case with mocking I am getting below errors

TEST CASE

  void "saveDomainObject success"() {
    setup:
    1 * domainDAO.saveDomain(domainObject) >> {DomainObject}
    Gson gson = new Gson();
    String json = gson.toJson(domainRequest);

    when:
    def resp = mockMvc.perform(post("/domain")
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .content(json)).andReturn().response
    def content = new JsonSlurper().parseText(resp.contentAsString)
    then:
    println content
  }

ERROR MESSAGE

Too few invocations for:

1 * domainDAO.saveDomain(domainObject) >> {DomainObject}   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * domainDAO.saveDomain(com.test.core.domain.DomainObject@6f27c0ef)


    at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at com.test.core.rest.service.DomainServiceServiceSpec.saveDomainObject success(DomainServiceSpec.groovy:132)

I am facing issues to test with save object, i tried with multiple options but still getting the same error. I doubt this is happening since domain object is in different jar, can any one help me to resolve the issue?

Thanks

Upvotes: 0

Views: 1211

Answers (1)

When you specify saveDomain(domainObject), you are telling Spock to expect the method to be called with the value in domainObject as the parameter. In your code, however, you're creating a new DomainObject and passing that to your mock. This is what I think you want:

1 * domainDAO.saveDomain(_ as DomainObject) >> { it[0] }

Upvotes: 1

Related Questions