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
Java Compiled Classes Contain Dollar Signs
How to Add Native Library to "Java.Library.Path" with Eclipse Launch (Instead of Overriding It)
Java Split() Method Strips Empty Strings at the End
How to Convert a .Jar to an .Exe
What Is the Meaning of $ in a Variable Name
What's the Right Way to Represent Phone Numbers
Removing Duplicates from a String in Java
How to Add Two Java.Lang.Numbers
What Is the Syntax of the Enhanced for Loop in Java
The Issue of * in Command Line Argument
How to Solve the Error "Not a Valid Xpath Expression"
Pkix Path Building Failed: Unable to Find Valid Certification Path to Requested Target
Converting JSON to Xml in Java
How to Convert Milliseconds to Date Format in Android