jeudi 2 avril 2015

Powermockito calling actual method when spying

I'm trying to mock a private method using PoweMockito. My actual code is running when I'm trying to tell PowerMockito to spy a method. From the below code, you can see the run method which calls initializeMongo is commented. But still initializeMongo is getting called when I run this test.



package mycompany.project.reader;

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({ ReaderMainTest.class })
public class ReaderMainTest {

public static String configFile = "conf/projectreader.test.xml";
private ReaderMain readerMain = null;

@Before
public void setUp() throws Exception {
readerMain = new ReaderMain(configFile);
}

@Test
public final void testRun() throws Exception {
ReaderMain readerSpy = PowerMockito.spy(readerMain);
PowerMockito
.doNothing()
.when(readerSpy,
PowerMockito
.method(ReaderMain.class, "initializeMongo"))
.withNoArguments();
// readerSpy.run();
}
}


Below is the constructor for ReaderMain. So it should not call the run method yet.



public ReaderMain(String configFile) {
this.configFile = configFile;
}


initializeMongo is a private method in ReaderMain with no arguments and returns void.


Aucun commentaire:

Enregistrer un commentaire