• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check in Mockito if method return correct File objects.  RSS feed

 
Mateusz Marek
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, so I was reading some tutorials, for example this one: http://www.vogella.com/tutorials/Mockito/article.html.
But I still have no idea how can I test if my function create correct files. Here is my code:



I need to test if createFile function return correct File objects. Can someone help me? I'm totally new in Mockito, never earlier was making anything in it. I tried to start like that:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If SomeClass is the class you want to test, then you shouldn't be mocking it. You use the mocking framework to create the collaborators of the class under test (CUT). This allows you to focus more on the CUT rather than have a bunch of extra code to prepare and set up the collaborators.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mockito is used to mock out dependencies to the class you are testing. You haven't posted your class so I can't comment on that, and the method you have posted does not have its dependencies injected. It is getting its dependencies using static factory methods. This means that you (probably) can't mock the dependencies to your method unless the static factory class allows the things your method is using to be set using a setter, which isn't a great design.

In general I would shy away from static factory methods, or at least shy away from using them directly in code that I need to unit test. It leads to code that is difficult to test.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!