At first glance, the answer is simple: well, there are several mocking frameworks, and there are different ways to use them.
The first example tells JUnit to use the “unit testing module” that the Mockito Mocking framework provides. The second example uses a unit testing module from PowerMock.
To make everything clear, you should also have different import statements, since both platforms have different implementations, for example, for @Mock annotation.
(The main point of using these framework-specific tests is that they take care of initializing all the fields with special annotations specific to the framework).
So, the difference here is simply that the first example is written using the Mockito infrastructure, and the second uses PowerMock.
Now which one to use?
Answer: Mokito.
What for? The ugly truth is that PowerMock-one is a cry for help. It says that "the test class is poorly designed, please fix it."
Meaning: As a developer, you can write "easy to test" code or "difficult to test" code. Many people do the second thing: they write code that is difficult to test. And then PowerMock (ito) provides tools for testing this code.
PowerMock (ito) gives you the ability to simulate (thus control) calls to static methods and new() . To enable this, PowerMock (ito) manipulates the byte code of the test code. This is great for small codebases, but when you come across millions of lines of production code and thousands of unit tests, it's completely different. I saw that many PowerMock tests failed for no apparent reason, several hours later, to find out that some kind of “static” thing was changed somewhere else, and this somehow affects another static / new managed PowerMock test case.
At some point, our team made an informed decision: when you write new code, and you can only check it with PowerMock ... this is unacceptable. Since then, we have been creating only Mockito test suites, and since then we have never had strange problems that interfered with us with PowerMock.
The only acceptable reason to use PowerMock is when you want to test existing (possibly third-party) code that you do not want to modify. But of course, what's the point of testing such code? If you cannot change this code, why should the tests fail suddenly?