
Explore the fundamentals of unit testing in Java with Mockito and Powermock, including mocking, Mockito API usage, inline module and BDD style, and coverage reporting with the JoCOCO Maven plugin.
As we know, the unit test is most important work, every developer engineer should write unit test code to verify the source code correctness before push the source code change to version control system like github, even the unit test code will be before the source code development work under the TDD methodology.
But how to write the correct and reasonable unit test code? actually as a software developer we have several principles can reference,let me explain those principles one by one.
A stuntman is a trained replacement used for dangerous action sequences in movies, such as a fight sequence on the top of a burning train, jumping from an airplane, and so on, mainly fight scenes. Stuntman are used to protect the real actors, are used when the actor is not available, or when the actor has a contract to not get involved in stunts.
totally speak, stuntman will help movies completed following plan timeline by cheap cost.
Very Similarly, sometimes we no need create the real external resources unit test for verify the source code correctness, mock the objects is good idea and a cheap way.
Configure the Mockito development environment by adding JUnit Jupiter, Mockito core, Mockito JUnit Jupiter, and optional Hamcrest to a Maven pom, then configure JUnit 5 engine and run mvn test.
Explore how Mockito deep mocks simulate external service factories and trigger runtime or null pointer exceptions in unit tests, and verify behavior with JUnit 5 executable assertions.
Explore Mockito stubbing statements such as when...thenReturn and doReturn...when, along with doNothing...when, when...thenThrow, and multiple return values on mocks.
Explore advanced Mockito stubbing with when...thenAnswer, doAnswer...when, and the Answer interface to simulate flexible mock behavior.
Explore the Mockito callRealMethod stubbing by using mocks to call real methods, switch a list to LinkedList, and illustrate partial mocking with PartialService and getRandom.
Explore Mockito stubbing statements such as when...thenReturn and doNothing...when, and learn which methods—void or non-void—are supported, with a preview of spy, capture arguments, and advanced features.
Master Mockito spying by using spy() on real objects and the @Spy annotation to create partial mocks that call real methods. Demonstrate with ArrayList examples, verifying items and list size.
Master how a Mockito spy enables a partial mock by keeping real getRandom() behavior and stubbing getFromExternal() with doReturn, while noting spying side effects and JUnit 5 @Spy limits.
Explore Mockito argument matcher usage to verify values with equals, anyInt, isA, and AdditionalMatchers, and learn composing matchers for complex stubbing and verification.
Implement a custom argument matcher in Mockito using an ArgumentsMatcher for a closed range. Define int begin and int end, provide a factory method closedRange, and verify behavior in tests.
Learn to use Mockito verify() to check which mock methods are invoked, as shown in testing UserService with saveOrUpdate(), updating for existing, and saving new ones.
Learn how to use Mockito verify with VerificationMode to tailor mock interactions using times, never, atLeastOnce, atLeast, atMost, and only, with argThat and anyString for argument matching.
Learn how to verify interactions in Mockito by using verifyNoInteractions, verifyNoMoreInteractions, and only to ensure correct method invocations on mocks like list1 and list2, including add and clear.
Learn why powermock makes code testable by mocking final and static elements, private methods, and local variables. Compare refactoring versus using whenNew to mock internal objects.
Set up the Powermock development environment with Powermock, Mockito, and Vintage for legacy JUnit code; configure the Maven plugin and argLine for reflection under JDK 16.
Learn how to quickly start with PowerMock to refactor and mock tests using PowerMock and Mockito. Cover setup steps, including the PowerMock runner, Servlet API dependency, and running Maven tests.
Explore how Powermock uses whenNew with arguments, no arguments, and anyArguments to stub constructors, mirroring Mockito when-then-return, and mock local variables for testability.
Master testing static methods with powermock by mocking SimpleDaoUtils static calls, returning a Simple object for getSimpleByName, and verifying the void saveSimple method using @PrepareForTest.
Master Powermock and Mockito to mock final classes and methods by testing a final dao's delete in a simple service, using whenNew and @PrepareForTest for byte-code manipulation.
JoCoCo introduction: learn about the free Java code coverage library for unit tests, with on-the-fly and offline instrumentation to collect coverage data and generate the report.
**** Please enable the vedio cc function (captions ) *****
Learn Java Unit Test: Mockito & Powermock Complete Tutorial contains below agenda, this course base on JAVA 16 and latest mockito 3.x version and latest powermock most detials guidance how to write the unit test code.
1. overview understand mocking
1.1 F.I.R.S.T Principles introduction
Fast
Isolated
Repeatable
Self-validating
Timely
1.2 what is mocking
1.3 simulate the user login application
1.4 unit test the user login function with mockito
2. Mockito details tutorial
2.1 how to mock objects
2.2 deep mock
2.3 mock object stubbling statement
when...thenReturn
doReturn...when
doNothing...when
when...thenThrow
doThrow...When
when...thenAnswer
doAnswer...when
when...thenCallRealMethod
doCallRealMethod...when
2.4 Stubbing statement - multiple return values
2.5 Stubbing statement - Cascading code style
2.6 Spying
2.7 Mockito argument matcher
2.8 verify method invocations
2.9 verify with VerificationMode
2.10 Mockito verify Interactions
2.11 Mockito ArgumentCaptor & @Captor
2.12 @InjectMocks
3. Mockito Inline Module
3.1 Mockito & BDD code style
3.2 Mockito mock static method
3.3 Mockito mock final classes & methods
4. Powermock details tutorial
4.1 what is powermock
4.2 why need powermock
4.3 powermock mock local variables
4.4 powermock mock static methods
4.5 powermock mock final classes & methods
4.6 powermock mock private methods
4.7 powermock spying
5. JoCoCo
5.1 JoCoCo for unit test coverage report on-the fly instrumentation
JoCoCo for unit test coverage report offline instrumentation