7

I have a Spring based application and am in the process of unit testing it. I'm using TestNG for unit tests. For my test i need to make use of PowerMockito to mock some static methods. I also need to make use of a test spring config file for only my unit test.

I'm unable to write my unit tests combining all the three i.e. TestNg, PowerMock and Spring.

I can combine TestNG and Spring by extending the class AbstractTestNGSpringContextTests, however cant mock static methods, instead it executes the actual static method. Something like the below:

@PrepareForTest(MyUtils.class)
@ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends AbstractTestNGSpringContextTests{

.....

}

I can combine TestNG with PowerMockito by extending the class PowerMockTestCase. But then the test spring config files are not resolved. Something like the below:

@PrepareForTest(MyUtils.class)
@ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends PowerMockTestCase{

.....

}

Is there any way for me to write my unit tests combining all the three, i.e. TestNg, PowerMockito and Spring context?

user320550
  • 1,093
  • 6
  • 20
  • 37

1 Answers1

0

Rather than extending PowerMockTestCase, have you tried using the PowerMockObjectFactory by writing a method like below? Then you can extend AbstractTestNGSpringContextTests.

@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new org.powermock.modules.testng.PowerMockObjectFactory();
}

This is suggested by the Powermock GitHub docs.

Stephen Hartley
  • 945
  • 1
  • 11
  • 17