How can I mock chained methods

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

How can I mock chained methods

Sharuru Z
Hi there, I am using Spock framework in my Java project and I have a question that document can not help :(

Here is the case:

In one of my validatorImpl, before returning, I do something with error message handler like this: 

context.buildConstraintViolationWithTemplate(msg).addPropertyNode(mstChkGroupCodeDocumentNo.clearingNo()).addConstraintViolation();

And absolutely, this method should be mocked when testing the main logic of validator.

I don't know how to mock this kind of chained methods calling with Spock.

Please help me, thank you.

--
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: How can I mock chained methods

Michael Kutz
First you should definitly think about your class structure and try to avoid these massive chains.

Anyway if you cannot avoid it, you can use this syntax to ease the pain of it:

context = Mock() { buildConstraintViolationWithTemplate(msg) >> Mock(...) { addPropertyNode(mstChkGroupCodeDocumentNo.clearingNo()) >> Mock(...) { addConstraintViolation() >> ... }

You will need to put in some classes in there (where I put "...") since Spock will not be able to reflect the return type of the mocked methods.

Sharuru Z <[hidden email]> schrieb am Mi., 15. Juni 2016 um 18:03 Uhr:
Hi there, I am using Spock framework in my Java project and I have a question that document can not help :(

Here is the case:

In one of my validatorImpl, before returning, I do something with error message handler like this: 

context.buildConstraintViolationWithTemplate(msg).addPropertyNode(mstChkGroupCodeDocumentNo.clearingNo()).addConstraintViolation();

And absolutely, this method should be mocked when testing the main logic of validator.

I don't know how to mock this kind of chained methods calling with Spock.

Please help me, thank you.

--
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: How can I mock chained methods

Sharuru Z
Problem solved.

Thank you for your advice on my class structure, I am working hard on it.

Have a nice day :)

在 2016年6月16日星期四 UTC+9下午3:56:23,Michael Kutz写道:
First you should definitly think about your class structure and try to avoid these massive chains.

Anyway if you cannot avoid it, you can use this syntax to ease the pain of it:

context = Mock() { buildConstraintViolationWithTemplate(msg) >> Mock(...) { addPropertyNode(mstChkGroupCodeDocumentNo.clearingNo()) >> Mock(...) { addConstraintViolation() >> ... }

You will need to put in some classes in there (where I put "...") since Spock will not be able to reflect the return type of the mocked methods.

Sharuru Z <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="h4rUnJqnBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">sharuru...@...> schrieb am Mi., 15. Juni 2016 um 18:03 Uhr:
Hi there, I am using Spock framework in my Java project and I have a question that document can not help :(

Here is the case:

In one of my validatorImpl, before returning, I do something with error message handler like this: 

context.buildConstraintViolationWithTemplate(msg).addPropertyNode(mstChkGroupCodeDocumentNo.clearingNo()).addConstraintViolation();

And absolutely, this method should be mocked when testing the main logic of validator.

I don't know how to mock this kind of chained methods calling with Spock.

Please help me, thank you.

--
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="h4rUnJqnBAAJ" 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="h4rUnJqnBAAJ" 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.