Spock seems mocking wrong fields

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

Spock seems mocking wrong fields

Sharuru Z
Hi, I am using Spock to test one of my service method.

Cause I want to do some interaction tests, I use mock to my repositories.

Code like this:

class ServiceSpec extends Specification {

@Inject
Service mService
@Inject
Service rService

OtherService otherService;
OtherRepository ;

def mockRepo(){
otherService = Mock();
otherRepository = Mock();
tblClearingRepository = Mock();
mService.@otherService = otherService;
mService.@otherRepository = otherRepository;
}

def "With mocked service and repository"() {
        when:
        def ret = mService.find(params)
       
        then:
        1 * otherRepository.find(parms) >> something
     }

     def "With real service and repository"() {
        when:
        def ret = rService.find(params)

        then:
        ret.amount = 100
    }
}



Thus rService's repository is real, it should get data from DB, but in fact, rServices's repository didn't get and data from DB.

It seems Spock is mocking and replacing all the method named Service class.

What should I do?
 

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Spock seems mocking wrong fields

Przemysław Wojnowski
If mService and rService are of the same type and the injected service is a singleton, which usually is the case, then by setting mService.@otherService/Repository you effectively change them in both mService and rService. That's why rService doesn't call the DB.
At least this is my guess. ;-)

2016-08-04 11:34 GMT+02:00 Sharuru Z <[hidden email]>:
Hi, I am using Spock to test one of my service method.

Cause I want to do some interaction tests, I use mock to my repositories.

Code like this:

class ServiceSpec extends Specification {

@Inject
Service mService
@Inject
Service rService

OtherService otherService;
OtherRepository ;

def mockRepo(){
otherService = Mock();
otherRepository = Mock();
tblClearingRepository = Mock();
mService.@otherService = otherService;
mService.@otherRepository = otherRepository;
}

def "With mocked service and repository"() {
        when:
        def ret = mService.find(params)
       
        then:
        1 * otherRepository.find(parms) >> something
     }

     def "With real service and repository"() {
        when:
        def ret = rService.find(params)

        then:
        ret.amount = 100
    }
}



Thus rService's repository is real, it should get data from DB, but in fact, rServices's repository didn't get and data from DB.

It seems Spock is mocking and replacing all the method named Service class.

What should I do?
 

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Spock seems mocking wrong fields

Sharuru Z
Thank you for your reply, is there anything I can do to solve this kind of problem?

在 2016年8月5日星期五 UTC+9下午7:51:28,Przemysław Wojnowski写道:
If mService and rService are of the same type and the injected service is a singleton, which usually is the case, then by setting mService.@otherService/Repository you effectively change them in both mService and rService. That's why rService doesn't call the DB.
At least this is my guess. ;-)

2016-08-04 11:34 GMT+02:00 Sharuru Z <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="8BQoMJqBCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">sharuru...@...>:
Hi, I am using Spock to test one of my service method.

Cause I want to do some interaction tests, I use mock to my repositories.

Code like this:

class ServiceSpec extends Specification {

@Inject
Service mService
@Inject
Service rService

OtherService otherService;
OtherRepository ;

def mockRepo(){
otherService = Mock();
otherRepository = Mock();
tblClearingRepository = Mock();
mService.@otherService = otherService;
mService.@otherRepository = otherRepository;
}

def "With mocked service and repository"() {
        when:
        def ret = mService.find(params)
       
        then:
        1 * otherRepository.find(parms) >> something
     }

     def "With real service and repository"() {
        when:
        def ret = rService.find(params)

        then:
        ret.amount = 100
    }
}



Thus rService's repository is real, it should get data from DB, but in fact, rServices's repository didn't get and data from DB.

It seems Spock is mocking and replacing all the method named Service class.

What should I do?
 

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="8BQoMJqBCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">spockframewor...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="8BQoMJqBCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">spockfr...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/spockframework" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/spockframework&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/spockframework&#39;;return true;">https://groups.google.com/group/spockframework.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.