Using PowerMock in Spring JUnit Test

Burak KILINC
Nov 2, 2022

--

Unit testing with help of a mocking framework has been recognized as a good practice for a long time and the Mockito framework.

First of all, we need to configure and add two related dependencies in order to use power mock.

Lets assume spring bean like this.

MyBeanClass.java

And if we want to test startProcessMyBeanClass method, in this test we assume that it’s important for us to mock a static call to IdGenerator.generateNewId(). However in order to run the test as spring integration test. we need to use SpringJunit4ClassRunner which prevents us from using the PowerMockRunner.

MyBeanClassTest.java

The main aim to use PowerMock is that the static methods can be tested in the unit test. There is a sample that points to the one controller in spring.

MyBeanClassTest.java last situation.

Thanks for reading..

--

--