Test Class with a New() Call in It with Mockito

Test class with a new() call in it with Mockito

For the future I would recommend Eran Harel's answer (refactoring moving new to factory that can be mocked). But if you don't want to change the original source code, use very handy and unique feature: spies. From the documentation:

You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

Real spies should be used carefully and occasionally, for example when dealing with legacy code.

In your case you should write:

TestedClass tc = spy(new TestedClass());
LoginContext lcMock = mock(LoginContext.class);
when(tc.login(anyString(), anyString())).thenReturn(lcMock);

Mocking new instance creation inside testing class using mockito

IMHO everything is better in your case than using Powermock, as you already stated.

In similar cases I am using a solution which puts the "unmockable" code in an as-small-as-possible method and @Spy around like:

public class SpyTest {
@Spy
private MyTestClass myTestClass;

@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}

@Test
public void spyInsteadOfPowermock() {
when(myTestClass.getObject()).thenReturn(Integer.valueOf(3));

assertThat(myTestClass.doSomethingMethod()).isEqualTo("3");
}

class MyTestClass{
public String doSomethingMethod(){
return getObject().toString();
}

Object getObject() {
return new Object();
}
}
}

Unit testing with mockito for constructors

Once again the problem with unit-testing comes from manually creating objects using new operator. Consider passing already created Second instead:

class First {

private Second second;

public First(int num, Second second) {
this.second = second;
this.num = num;
}

// some other methods...
}

I know this might mean major rewrite of your API, but there is no other way. Also this class doesn't have any sense:

Mockito.when(new Second(any(String.class).thenReturn(null)));

First of all Mockito can only mock methods, not constructors. Secondly, even if you could mock constructor, you are mocking constructor of just created object and never really doing anything with that object.



Related Topics



Leave a reply



Submit