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

grails Unit test spock issue

From Dev

Spock Mock not working for unit test

From Dev

Angular Unit test for an injected service

From Dev

Asserting view with Grails Spock Unit Test for Controllers

From Dev

How to test Grails service using Spock?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Using Spock data driven tests in Grails 3 unit test

From Dev

Grails Unit Test Service MissingProperty 'log'

From Dev

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

From Dev

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

From Dev

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

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

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

From Dev

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

From Dev

Spock fails when PostConstruct calls a injected service

From Dev

Grails Spock testing Controller and service

From Dev

Spock Testing a POST Service in Grails

From Dev

Injected Dependency is undefined in Unit Test

From Dev

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

Related Related

  1. 1

    grails Unit test spock issue

  2. 2

    Spock Mock not working for unit test

  3. 3

    Angular Unit test for an injected service

  4. 4

    Asserting view with Grails Spock Unit Test for Controllers

  5. 5

    How to test Grails service using Spock?

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    Using Spock data driven tests in Grails 3 unit test

  16. 16

    Grails Unit Test Service MissingProperty 'log'

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    how to mock $window to unit test AngularJS service?

  21. 21

    How to mock AngularFire 2 service in unit test?

  22. 22

    How to mock AngularFire 2 service in unit test?

  23. 23

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

  24. 24

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

  25. 25

    Spock fails when PostConstruct calls a injected service

  26. 26

    Grails Spock testing Controller and service

  27. 27

    Spock Testing a POST Service in Grails

  28. 28

    Injected Dependency is undefined in Unit Test

  29. 29

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

HotTag

Archive