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 ...