TestNG + Spring + Power mock Unit test

I have a Spring based application and am in the process of unit testing. I use TestNG for unit tests. For my test, I need to use PowerMockito to make fun of some static methods. I also need to use the Spring test configuration file for my unit test only.

I cannot write my unit tests combining all three, i.e. TestNg, PowerMock and Spring.

I can combine TestNG and Spring by extending the AbstractTestNGSpringContextTests class, but, be that as it may, mocks the static methods, instead it executes the actual static method. Something like 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 PowerMockTestCase class. But then the Spring test configuration files are not allowed. Something like below:

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

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

+5
source share
1 answer

Instead of extending PowerMockTestCase , have you tried using PowerMockObjectFactory by writing a method as shown below? Then you can extend AbstractTestNGSpringContextTests .

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

This is the Powermock GitHub Documentation .

0
source

Source: https://habr.com/ru/post/1239949/


All Articles