Grails Spock Mock Injected Service in Unit Test

biniam

How do I inject another service in a Grails UnitTest (Specification) using Spock?

class ServiceOne {

   ServiceTwo serviceTwo

    ModelOne getMethodOne(String name) {
       // do somethings here
       return serviceTwo.getMethodTwo(name)
    }
}

class ServiceTwo {

   ServiceTwo serviceTwo

    ModelOne getMethodTwo(String name) {
       // do somethings here
       return ModelOne.get(name)
    }
}

// Tests
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
 * Tests for ServiceOne
 */
@TestFor(ServiceOne)
@Mock(ModelOne)
class ServiceOneSpec extends Specification {
/** Inject serviceTwo in here
    Otherwise the following exception is thrown
    java.lang.NullPointerException: Cannot invoke method getMethodTwo() on null object
*/

void "test method one"() {

    when:
    ModelOne modelOne = service.getMethodOne(name)

    then:
    modelOne != null
   }
}
biniam

This is how I solved my question - by creating an object of the Service (It is not singleton but it is fine since it is just for unit testing)

// Test Spec
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
 * Tests for ServiceOne
 */
@TestFor(ServiceOne)
@Mock(ModelOne)
class ServiceOneSpec extends Specification {

    @Shared ModelOne modelOne
    def setup() {
        // the magic
        service.serviceTwo = new ServiceTwo()
        modelOne = service.serviceTwo.getMethodTwo("Name")
    }

    void "test method one"() {

        when:
        ModelOne modelOne = service.getMethodOne(name)

        then:
        modelOne != null
   }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to test Grails service using Spock?

From Dev

How to write a spock unit test case for traits in grails 2.4?

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

Grails Unit Test Service MissingProperty 'log'

From Dev

grails Unit test spock issue

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

Grails: how to save value to transient field while unit test with spock?

From Dev

Unable to mock Grails Service method when unit testing Controller - MissingMethodException

From Dev

Grails Spock testing Controller and service

From Dev

Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

From Dev

Spock Mock not working for unit test

From Dev

Injected Dependency is undefined in Unit Test

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

From Dev

Asserting view with Grails Spock Unit Test for Controllers

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

Grails: how to save value to transient field while unit test with spock?

From Dev

Unable to mock Grails Service method when unit testing Controller - MissingMethodException

From Dev

How to use spock to test a service with real transactions in Grails application?

From Dev

How to test the catch block code in Spock unit test Grails

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

Using Spock data driven tests in Grails 3 unit test

From Dev

Grails Can't inject service to domain when test with Spock

From Dev

Spock Testing a POST Service in Grails

From Dev

How to mock an interface parameter for a service method using Spock and Grails 2?

From Dev

Spock fails when PostConstruct calls a injected service

From Dev

Mock a controller for unit test without any service reference C#

From Dev

Angular Unit test for an injected service

Related Related

  1. 1

    How to test Grails service using Spock?

  2. 2

    How to write a spock unit test case for traits in grails 2.4?

  3. 3

    Correct way to mock an AngularJS Service in a unit test?

  4. 4

    Grails Unit Test Service MissingProperty 'log'

  5. 5

    grails Unit test spock issue

  6. 6

    how to mock $window to unit test AngularJS service?

  7. 7

    How to mock render in (Spock, Grails) unit testing Taglib?

  8. 8

    Grails: how to save value to transient field while unit test with spock?

  9. 9

    Unable to mock Grails Service method when unit testing Controller - MissingMethodException

  10. 10

    Grails Spock testing Controller and service

  11. 11

    Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

  12. 12

    Spock Mock not working for unit test

  13. 13

    Injected Dependency is undefined in Unit Test

  14. 14

    How to mock AngularFire 2 service in unit test?

  15. 15

    Untestable grails (2.5.4) service using @PostConstruct with Spock unit testing

  16. 16

    Asserting view with Grails Spock Unit Test for Controllers

  17. 17

    How to mock render in (Spock, Grails) unit testing Taglib?

  18. 18

    Grails: how to save value to transient field while unit test with spock?

  19. 19

    Unable to mock Grails Service method when unit testing Controller - MissingMethodException

  20. 20

    How to use spock to test a service with real transactions in Grails application?

  21. 21

    How to test the catch block code in Spock unit test Grails

  22. 22

    How to mock AngularFire 2 service in unit test?

  23. 23

    Using Spock data driven tests in Grails 3 unit test

  24. 24

    Grails Can't inject service to domain when test with Spock

  25. 25

    Spock Testing a POST Service in Grails

  26. 26

    How to mock an interface parameter for a service method using Spock and Grails 2?

  27. 27

    Spock fails when PostConstruct calls a injected service

  28. 28

    Mock a controller for unit test without any service reference C#

  29. 29

    Angular Unit test for an injected service

HotTag

Archive