Spock unit test service tha use other services

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Spock unit test service tha use other services

Luca Farsetti
Hello all, i finally play with Spock (and TDD) and i have a issue.
I have a service (call A) that call another service (B), and when i test the method of service A that use the service B i have a
java.lang.NullPointerException: Cannot invoke method methodName() on null object..
The null object it's the B service of course, but i can't understan why it's null.

this is my spock unit test method

void "test save method"() {
        setup:
            def aService = new AService()
            def bService = new BService()
            mockDomain(ADomain)
            mockDomain(BDomain)       
       
        when:
            aService.bService = bService
            def instance = aService.create("username", "conto",  12.8d, "causale")
           
        then:
            def e = thrown(AException)
            e.message == "Unable to save a"
            e.aDomainInstance != null

    }

I have tryed other way for define BService instance, like

...
mockDomain(BService, [bServiceInstance]

when:
   aService.bService = bServiceInstance
...
where:
   bServiceInstace = new BService()


but without success

Someone could help me?

Best regard

Luca
Reply | Threaded
Open this post in threaded view
|

Re: Spock unit test service tha use other services

Nathan Wells
Take a look at Spock Interactions:


Basically, it provides an API to Mock out dependencies. Using mockDomain will likely not work, and certainly won't allow the level of testing that Interactions does. Let us know if you have any other questions.

Nathan Wells


On Thu, Nov 22, 2012 at 6:31 AM, Luca Farsetti <[hidden email]> wrote:
Hello all, i finally play with Spock (and TDD) and i have a issue.
I have a service (call A) that call another service (B), and when i test the method of service A that use the service B i have a
java.lang.NullPointerException: Cannot invoke method methodName() on null object..
The null object it's the B service of course, but i can't understan why it's null.

this is my spock unit test method

void "test save method"() {
        setup:
            def aService = new AService()
            def bService = new BService()
            mockDomain(ADomain)
            mockDomain(BDomain)       
       
        when:
            aService.bService = bService
            def instance = aService.create("username", "conto",  12.8d, "causale")
           
        then:
            def e = thrown(AException)
            e.message == "Unable to save a"
            e.aDomainInstance != null

    }

I have tryed other way for define BService instance, like

...
mockDomain(BService, [bServiceInstance]

when:
   aService.bService = bServiceInstance
...
where:
   bServiceInstace = new BService()


but without success

Someone could help me?

Best regard

Luca

Reply | Threaded
Open this post in threaded view
|

RE: Spock unit test service tha use other services

jonspalmer
In reply to this post by Luca Farsetti

It’s a little hard to understand what you are trying to test. Could you maybe tell us a bit more about that and maybe provide the implementation of aService.create that you are trying to test?

In particular in you spec is it important that you assert that some method on bService gets called? If so  you should look at the section on interaction in the Spock docs: http://docs.spockframework.org/en/latest/interaction_based_testing.html In particular the use of Mock to mock collaborators.

 

Jon

 

From: Luca Farsetti [mailto:[hidden email]]
Sent: Thursday, November 22, 2012 8:32 AM
To: [hidden email]
Subject: [grails-user] Spock unit test service tha use other services

 

Hello all, i finally play with Spock (and TDD) and i have a issue.
I have a service (call A) that call another service (B), and when i test the method of service A that use the service B i have a
java.lang.NullPointerException: Cannot invoke method methodName() on null object..
The null object it's the B service of course, but i can't understan why it's null.

this is my spock unit test method

void "test save method"() {
        setup:
            def aService = new AService()
            def bService = new BService()
            mockDomain(ADomain)
            mockDomain(BDomain)       
       
        when:
            aService.bService = bService
            def instance = aService.create("username", "conto",  12.8d, "causale")
           
        then:
            def e = thrown(AException)
            e.message == "Unable to save a"
            e.aDomainInstance != null

    }

I have tryed other way for define BService instance, like

...
mockDomain(BService, [bServiceInstance]

when:
   aService.bService = bServiceInstance
...
where:
   bServiceInstace = new BService()


but without success

Someone could help me?

Best regard

Luca



This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.