Mock static method in Unit Test using PowerMock

In this tutorial, I will guide you to mock static methods in Unit Test using PowerMock!

If you do not know about mock in the Unit Test, I can say it like this: Mock is a solution that helps us to create a mock object so that we can specify the behavior of an object in Unit Test. For example, you have an object that connects to the SFTP server but in the Unit Test you cannot set up a real SFTP server, so you cannot test the connection function of this object to the SFTP server. In this case, you can create a mock object that simulates the behavior of the connection object to the SFTP server and specifies that when the other object calls to the method making the connection to SFTP of this object, it will return a value.

Why do we have to use PowerMock to mock static methods? Simply because there is only PowerMock support for us to do this.

OK, now let’s go to the main topic of this tutorial!

I will create a Maven project as an example for you to understand. My project is as follows:

Mock static method in Unit Test using PowerMock

We will now add the necessary dependencies of JUnit and PowerMock to our project. Open the pom.xml file and add the following dependencies:

And the class containing the static method that we need to mock, will have something like this:

Mock static method in Unit Test using PowerMock

The purpose of this class is to use Java to execute a command line that checks the existence of a file on another machine using SSH without having to use a user and password to log in (to run a program using this class, we have to set the login to another machine without user and password).

As you can see, in this class, I used the Runtime object with the getRuntime() static method to execute a command line in Java. In Unit Test, how can we simulate another machine to test the checkFileExistingWithoutUsernamePassword() static method in the SSHCommandLineHelper class? Our solution is to use mock only.

First, create a SSHCommandLineHelperTest class in the test directory with the package as com.huongdanjava.mockstatic as follows:

Mock static method in Unit Test using PowerMock

In this class, first declare annotation @PrepareForTest and @RunWith first as follows:

In the @Before section of the Unit Test, we will declare using PowerMock to mock the static method in the Runtime class as follows:

Now, we will write a test case for when we run the program to check if the file exists on another SSH machine, the checkFileExistingWithoutUsernamePassword() static method returns the value TRUE!

The contents of the testCheckFileExistingSuccessWithoutUsernamePassword() method are as follows:

Result:

Mock static method in Unit Test using PowerMock

In case we test for non-existent files, just modify a little as follows:

Result:

Mock static method in Unit Test using PowerMock

The code for the SSHCommandLineHelperTest class is as follows:

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment