Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to mock environment variable in unit test using Spock?

 
Ranch Hand
Posts: 85
2
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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:
 
Sheriff
Posts: 15754
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficulty in testing often is a design smell. Without reading your code and just a few sentences of your problem description I can already smell the mixing of concerns. Why do you need to mock the environment? Why does your service care where it's getting credentials? These are separate concerns. I would first refactor so I can test the service independently from the source of the credentials.
 
Junilu Lacar
Sheriff
Posts: 15754
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also see problems in your code. In your DriveUtils, the getCredentials method is private and takes no parameters. In your test you try to call it with a parameter.

Also your try to get "mockCredentials" environment variable in the test but the DriveUtils class has a hard-coded environment variable name so how does your "mockCredentials" name even come into play here?

The design seems confused and therefore I'm not surprised that the test would also be confusing. Confusion in your design usually leads to things not working the way they're expected to work.
 
Junilu Lacar
Sheriff
Posts: 15754
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Asif Haciyev
Ranch Hand
Posts: 85
2
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Asif Haciyev
Ranch Hand
Posts: 85
2
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
    Bookmark Topic Watch Topic
  • New Topic