java - Unit testing with mockito for constructors -


i have 1 class.

class first {      private second second;      public first(int num, string str) {         second = new second(str);         this.num = num;     }      ... // other methods } 

i want write unit tests public methods of class first. want avoid execution of constructor of class second.

i did this:

second second = mockito.mock(second.class); mockito.when(new second(any(string.class).thenreturn(null); first first = new first(null, null); 

it still calling constructor of class second. how can avoid it?

once again problem unit-testing comes manually creating objects using new operator. consider passing created second instead:

class first {    private second second;    public first(int num, second second) {     this.second = second;     this.num = num;   }    // other methods... } 

i know might mean major rewrite of api, there no other way. class doesn't have sense:

mockito.when(new second(any(string.class).thenreturn(null))); 

first of mockito can mock methods, not constructors. secondly, if mock constructor, mocking constructor of created object , never doing object.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -