I got the problem that after mocking the java.io.File(File parent, String childName) constructor with PowerMock still the original constructor is invoked.
Here is the coding:
Class under test:
import java.io.File;
public class ConstructChildFile {
public File newChildFile(File parent, String childName) {
return new File(parent, childName);
}
}
Test case:
import java.io.File;
import org.easymock.EasyMock;
import org.junit.Test;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
#PrepareForTest({File.class, ConstructChildFile.class})
public class TestConstructorMocking {
#Test
public void test() throws Exception {
File mockedFolder = EasyMock.createMock(File.class);
File mockedChild = EasyMock.createMock(File.class);
PowerMock.expectNew(File.class, mockedFolder, "test").andReturn(mockedChild).anyTimes();
EasyMock.replay(mockedFolder, mockedChild);
PowerMock.replay(File.class, ConstructChildFile.class);
System.out.println(new ConstructChildFile().newChildFile(mockedFolder, "test"));
}
}
So when ConstructChildFile.newChildFile(...) is invoked I expect to get the mockedChild instance since I mocked the constructor a few lines above. Still this does not happen - the actual constructor is called.
The test fails with:
java.lang.NullPointerException
at java.io.File.<init>(File.java:363)
at test.ConstructChildFile.newChildFile(ConstructChildFile.java:7)
at test.TestConstructorMocking.test(TestConstructorMocking.java:24)
...
Any ideas?
Related
I cannot mock a method defined as default in an interface. Can anyone help me here?
The interface has default method providing a logger.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public interface Loggable {
default Logger logger() {
return LoggerFactory.getLogger(this.getClass());
}
}
It is used this way:
public class AppShowOff implements Loggable{
public void doMagic() {
logger().debug("It works");
System.out.println("Works");
}
}
now I would like to write a test proving that debug method has been called.
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
public class AppShowOffTest {
#Test
public void doMagic() {
Logger loggerMock = mock(Logger.class);
Loggable loggableMock = mock(Loggable.class); // <- not needed, but I also tried this way
// mocks done
AppShowOff app = new AppShowOff();
AppShowOff appSpy = Mockito.spy(new AppShowOff());
when(loggableMock.logger()).thenReturn(loggerMock);
when(appSpy.logger()).thenReturn(loggerMock);
app.doMagic();
verify(loggerMock, times(1)).debug(any());
}
}
as you can see I have tried to mock the default method in two ways:
when(loggableMock.logger()).thenReturn(loggerMock);
when(appSpy.logger()).thenReturn(loggerMock);
but it does not work. The result is:
Wanted but not invoked: logger.debug();
-> at so.AppShowOffTest.doMagic(AppShowOffTest.java:29) Actually, there were zero interactions with this mock.
Here:
AppShowOff app = new AppShowOff();
AppShowOff appSpy = Mockito.spy(new AppShowOff());
That first app ... is never used, besides to call your method under test doMagic().
The simple answer: drop app completely, and invoke appSpy.doMagic().
I have a final class as below
public class firstclass{
private String firstmethod(){
return new secondclass("params").somemethod();
}
}
public final class secondclass{
secondclass(String params){
//some code
}
public String somemethod(){
// some code
return somevariable";
}
}
I have to here test first class so I have mocked this as below
secondclass classMock = PowerMockito.mock(secondclass .class);
PowerMockito.whenNew(secondclass .class).withAnyArguments().thenReturn(classMock);
Mockito.doReturn("test").when(classMock).somemethod();
But it is not mocking as I expected can anyone help me?
The method firstclass.firstmethod() is private method. So try to test this method through public method in which it is getting called.
You can mock SecondClass and its final method using #RunWith(PowerMockRunner.class) and #PrepareForTest(SecondClass.class) annotations.
Please see below the working code:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(SecondClass.class)
public class FirstClassTest{
#Before
public void init() {
}
#After
public void clear() {
}
#Test
public void testfirstmethod() throws Exception{
SecondClass classMock = PowerMockito.mock(SecondClass.class);
PowerMockito.whenNew(SecondClass.class).withAnyArguments().thenReturn(classMock);
Mockito.doReturn("test").when(classMock).somemethod();
new FirstClass().firstmethod();
}
}
Libraries used:
I want to cover a logic, that creates files with unit tests. Is it possible to mock File class and to avoid actual file creation?
Mock the constructor, like in this example code. Don't forget to put the class that will invoke the "new File(...)" in the #PrepareForTest
package hello.easymock.constructor;
import java.io.File;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({File.class})
public class ConstructorExampleTest {
#Test
public void testMockFile() throws Exception {
// first, create a mock for File
final File fileMock = EasyMock.createMock(File.class);
EasyMock.expect(fileMock.getAbsolutePath()).andReturn("/my/fake/file/path");
EasyMock.replay(fileMock);
// then return the mocked object if the constructor is invoked
Class<?>[] parameterTypes = new Class[] { String.class };
PowerMock.expectNew(File.class, parameterTypes , EasyMock.isA(String.class)).andReturn(fileMock);
PowerMock.replay(File.class);
// try constructing a real File and check if the mock kicked in
final String mockedFilePath = new File("/real/path/for/file").getAbsolutePath();
Assert.assertEquals("/my/fake/file/path", mockedFilePath);
}
}
try PowerMockito
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
#PrepareForTest(YourUtilityClassWhereFileIsCreated.class)
public class TestClass {
#Test
public void myTestMethod() {
File myFile = PowerMockito.mock(File.class);
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(myFile);
Mockito.when(myFile.createNewFile()).thenReturn(true);
}
}
I'm not sure if it's possible, but I have had such a requirement, and I solved it by creating a FileService interface. So instead of creating/accessing files directly, you add an abstraction. Then you can easily mock this interface in your tests.
For instance:
public interface FileService {
InputStream openFile(String path);
OutputStream createFile(String path);
}
Then in your class using this:
public class MyClass {
private FileService fileService;
public MyClass(FileService fileService) {
this.fileService = fileService;
}
public void doSomething() {
// Instead of creating file, use file service
OutputStream out = fileService.createFile(myFileName);
}
}
And in your test
#Test
public void testOperationThatCreatesFile() {
MyClass myClass = new MyClass(mockFileService);
// Do your tests
}
This way, you can even mock it without any mock libraries.
I have a DummyResource class and a DummyTarget file, and a test class TestDummyResource as below, but the mocked object DummyResource dr = mock(DummyResource.class) only works when I call the constructor inside a normal class, when it's called in an anonymous class, it's calling the actual constructor instead of using the mocked object.
Versions:
powermock 1.4.12 mockito 1.9.0 junit 4.8.2
DummyTarget.java:
import java.io.IOException;
import java.io.OutputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
public class DummyTarget {
public StreamingOutput testMocking() {
return new StreamingOutput() {
#Override
public void write(OutputStream arg0) throws IOException, WebApplicationException {
new DummyResource();
}
};
}
}
DummyResource.java:
package com.smin.dummy;
public class DummyResource {
public DummyResource() {
System.out.println("mock failure");
}
}
TestDummyResource.java:
package com.smin.dummy;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.StreamingOutput;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({DummyResource.class,DummyTarget.class})
public class TestDummyResource {
#Before
public void setUp() throws Exception {
DummyResource dr = mock(DummyResource.class);
PowerMockito.whenNew(DummyResource.class).withNoArguments().thenReturn(dr);
}
#Test
public void testMocked() throws WebApplicationException, IOException {
new DummyResource(); // it uses the mocked object dr here,
//doesn't print "mock failure"
StreamingOutput sop = new DummyTarget().testMocking();
sop.write(null); // it calls DummyResource's constructor,
// prints ""mock failure"" here
}
}
You need to have prepared the class calling the constructor, not the class on which the constructor is called, the following should fix you up:
#PrepareForTest(DummyTarget.class)
For more information check this page.
It looks like an anonymous class may inherit the package of the class that defines it. Can you try the wildcard form of PrepareForTest?:
#PrepareForTest("com.smin.dummy.*")
If that doesn't work, you could try the shotgun PrepareEverythingForTest Annotation.
Actually, you have to prepare for test the class that makes the constructor call, not the class on which the constructor was called. See https://github.com/jayway/powermock/wiki/MockConstructor.
In your case, you should use #PrepareForTest(DummyTarget.class)
I had the same problem, and resolved it with using whenNew with fully qualified name. The fully qualified name of an inner anonymous class in your case is:
DummyTarget.class + "$1"
so you should create a mock of that class:
DummyResource dr = mock(Class.forName(DummyTarget.class + "$1"));
and it will work for you.
Also, don't forget to prepare the DummyTarget class:
#PrepareForTest(DummyTarget.class)
Hope it helped =]
MyClass.java:
package test;
public final class MyClass {
public MyClass() { }
public Package returnPackage() {
return MyClass.class.getPackage();
}
}
TestClass.java:
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.mockito.PowerMockito.*;
#RunWith(PowerMockRunner.class)
#PrepareForTest(MyClass.class)
public class TestClass {
#Test
public void test() throws Exception {
System.out.println(new MyClass().returnPackage());
mockStatic(MyClass.class);
System.out.println(new MyClass().returnPackage());
}
}
However, this results in:
package test
null
Any MyClass.class method call returns null after PowerMockito.mockStatic(Class). Is there any way to prevent this?
Workaround: I can use the EasyMock-API to set up the static mocks without PowerMockito.mockStatic().