In this post under Powermock, I will with example show how to set private fields of a class for unit testing. For example, consider you have a class as shown below to be tested Class To Be Tested package package5; import java.util.Date; import java.util.Random; public class Example5 { private Random random = new Random(); public…… Continue reading Setting private fields of a class for unit testing
Category: PowerMock
Stubbing final methods
This post explains how to stub a final method. The class to be tested is as shown below Class1 package package5; public class Class1 { public final int method1(int value) { return value * value; } } The test class is as shown below Test Class 1 package package5; 2 3 import org.junit.Before; 4 import…… Continue reading Stubbing final methods
Verifying private method invocation
This post explains how to write an unit test case, to verify a Class’s private method is invoked. We invoke a Class’s private method indirectly through the help of public method exposed by the class. When writing unit test cases for public methods we want to make sure that private methods with expected arguments is…… Continue reading Verifying private method invocation
Stubbing private methods
This post explains how to stub private methods of a class using PowerMock. Lets consider a class named “Class1” with the structure as shown below Classes to be tested package package3; public class Class1 { private int method1(int value1) { return value1 + 2; } public int method2(int value2) { return this.method1(value2); } } We…… Continue reading Stubbing private methods
Mocking Static class methods
In Mockito we cannot mock static class methods, so we can use PowerMock which extends the Mockito features and provides its own features. One of which is mocking static class methods. Below code will give an example how to mock static class methods Calculator package package1; public class Calculator { public static int add(int value1,…… Continue reading Mocking Static class methods