This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Asif Haciyev

Ranch Hand
+ Follow
since Oct 24, 2018
Asif likes ...
Netbeans IDE Oracle Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Asif Haciyev

Junilu Lacar wrote:Trying to decipher your test code to understand its intent, I see the when part exercising the method under test, getImages(). The expectation is that you'll get 3 items. In the given section, you set up four files, three of which are similar and one different. I assume the three items you expect to get from the method under test are the similarly configured ones in the given section, right?

This is not easy to discern and suggests that your API can be made more intuitive so that the test code can clearly express what the intent is.

Yes, one of them is different because I expect non-trashed images. And I check it in my method, API brings all files currently.
1 month ago
Yes, I'm sorry. getCredentials  method has no parameters actually and I forgot to delete in test method. But result is same.
Service method which I try to test is this:

This method doesn't directly need Drive object (which returned from DriveUtil.getDrive() method) but inside of it there is getFilesInParticularFolder(folderName, mimeType) method. It needs drive object:

1 month ago
I am using Spock for unit testing. And now I need to create mock environment variable. I tried some methods, for example

setting it in setup() method and in static block also, but it didn't work. More clearly, I am using Google Drive for storing files. For making http request it needs credentials and I defined these credentials as environment variables. So, in unit test I want to test my service methods. For example, getting files from particular folder. And the problem is here: test method use real credentials for getting files and therefore it brings real files from drive. And I think this behavior is not of unit test's. So, how can I create mock object for environment variable?
Drive Util class:

Test class:
1 month ago
Hi, there is a situation I encountered. There are some methods a(), b(), c(), d(), e(). And each method can throw exception, I call all methods one by one and here I need that if one method throws exception others have to work. I don't know which method will throw beforehand. For example,

Here if method a throws exception others (b, c, d, e) have to work, if b throws(I assume a is worked already) c, d, e have to work. I thought one solution

but there is problem too, if b is failed in catch block c, d, e will not work. After that I though another way: I can write inner "try catch" (I mean inside catch there is also try-catch and inside this catch another try-catch and so on). But it doesn't solve the problem, because if method count will increase the try-catch block will be longer too. How can I solve this problem?
3 months ago
Thank you both, Junilu and Tim. I am sorry, I couldn't reply you until now.
3 months ago

Tim Cooke wrote:Even just removing the interaction assertion makes it work ok for me. Makes no sense.

I also asked same question Junilu. Will it be right test or it is just working without exception?
4 months ago

Junilu Lacar wrote:I've only been able to make Tim's test work if I change Mock() to Stub() and remove the interaction check. But that kind of changes the test.

Yes it will work. And I also mentioned that but even not changing Mock() to Stub() test is passing. But I don't know it is right or not. Let's say if I remove the interaction then test will pass, but will it be right test or just method works without exception. Also do I need change Mock() to Stub() actually.

Asif Haciyev wrote:Also, when I change this

to this

the test passed.

Junilu Lacar wrote:I put in a bunch of checks but the baffling thing is that somehow if you use Mock(), the stubbing gets clobbered when you make the critical call to getAllMembers() -- it seems as if the mocked findAll() method exhibits default behavior of returning null all of a sudden even though you still have the same mock object (I verified this using hashCode())

And I am sorry, I didn't get the second part of your answer.
4 months ago

Junilu Lacar wrote:My understanding from what I read in the documentation is that the "1 *" causes it to be interpreted as an expected interaction vs. just a plain old method call.

So, in my case I need 1 * memberRepository.findAll() to be sure method is calling at least once. Right?
4 months ago

Junilu Lacar wrote:Did you double-check that memberRepository is not null?

Yes, I checked, it is not null. I print it out on console, it shows Mock for type 'MemberRepository' named 'memberRepository'

Junilu Lacar wrote:
What does the service constructor look like? Just want to make sure all the basic assumptions are correct.

With that code, we're assuming that the constructor properly sets a memberRepository field.

If that checks out, I would try moving those two lines to given block to see if that makes a difference. I know the setup() block should work but again, I would just want to eliminate these as possible causes.

I did what you said. I commented out setup() method and moved these two lines to given block. But it didn't make sense
4 months ago

Stephan van Hulst wrote:

Asif Haciyev wrote:.../index.html file contains this

That's just the HTML to show the test report. If you open it in a browser it should show you the actual test report.

I checked it. It is just a test summary, like:
1 test, 1 failure, 0 ignore, 0.879s duration, 0% successful, package and class names.
Similar to "run test with coverage".
4 months ago

Junilu Lacar wrote:In your code, what does the data being piped get assigned to? Does it take everything in the right hand side and put it into the members array/collection? Have you tried debugging to check that members is not null?

Yes, it adds object on the right side to the list on the left side. From documentation
class PublisherSpec extends Specification {
   Publisher publisher = new Publisher()
   Subscriber subscriber = Mock()
   Subscriber subscriber2 = Mock()

   def setup() {
       publisher.subscribers << subscriber // << is a Groovy shorthand for List.add()
       publisher.subscribers << subscriber2

Yes I did, it is not null. I print it out and it shows that list has objects which I have added.
4 months ago

Stephan van Hulst wrote:Is there no stack trace at /ms-team/build/reports/tests/test/index.html?

.../index.html file contains this
4 months ago

Tim Cooke wrote:Without running your code, which I can't do right now, I'd say it's most likely because you have not instructed the mock memberRepository object to return anything when findAll() method is called. I recommend you double check the syntax for doing that with Spock.

From documentation
Returning Fixed Values
We have already seen the use of the right-shift (>>) operator to return a fixed value:
subscriber.receive(_) >> "ok"

So I think memberRepository.findAll() >> mockMembers is right and that will return fixed mockMembers list

Tim Cooke wrote:
This looks highly suspicious to me, especially the MemberMapper line. It looks like you're defining Spock like instructions on an object that is not a Spock mock object so mockMembers is unlikely to be in the state you expect.

I changed dto to entity object to get rid of that line which you mentioned. Now I just add entity object to list and say memberRepository.findAll() should return that list. But the problem still exist.
4 months ago

Tim Cooke wrote:Alright, entityToDtoList(null) returns null. Now let's look at your code to see if that behaviour is handled appropriately:

Yes, I agree.
But my question is why here it is null entityToDtoList(null), why memberRepository.findAll() returns null. I set this in test method. And it is not null there, it has mockMembers list and I print it out in test method, it returns what it has to (mockMembers list), but it goes as a null to service method.
4 months ago
isEmpty() method is calling on null object? and it is not possible.
4 months ago