mockito: Mockito 5 lab (inline mock maker, static/construction mocking, captors, BDD, strict stubs)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cfo2K4u4rF18QsPXPFnytf
This commit is contained in:
2026-10-24 17:19:45 +00:00
co-authored by Claude Sonnet 5.5
commit 8fc8bc934f
38 changed files with 689 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
default MockMaker : org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker
PriceCalculator is final : true
mock class name : lab.mockito.PriceCalculator
mock class == real class : true
stubbed final method : applyDiscount(1000, 10) = 42
unstubbed final method : applyDiscount(1000, 20) = 0
mocked a record : true
+4
View File
@@ -0,0 +1,4 @@
before scope : id starts with ORD- : true
inside scope : place("alice") = ORD-FIXED@alice
inside scope : unstubbed Ids.nowMillis() = 0 (default answer, not the clock)
after scope : id starts with ORD- and is not FIXED : true
@@ -0,0 +1,2 @@
test thread sees : ORD-FIXED
other thread sees : real id, not FIXED = true
@@ -0,0 +1,4 @@
MockitoException:
For lab.mockito.Ids, static mocking is already registered in the current thread
To create a new mock, the existing static mock registration must be deregistered
@@ -0,0 +1,2 @@
newOrderId() = ORD-FIXED
nowMillis() is the real clock (> 0) : true
@@ -0,0 +1,2 @@
MockitoException:
It is not possible to mock static methods of java.lang.System to avoid interfering with class loading what leads to infinite loops
+6
View File
@@ -0,0 +1,6 @@
no interception : real SMTP connection attempted to smtp.example.com
constructor args : [smtp.example.com]
construction # : 1
welcome("bob") : true
instances built : 1
verified : send("[email protected]", "Welcome, bob")
+3
View File
@@ -0,0 +1,3 @@
charged amounts : [900, 500]
last captured : 500
audit details : [acc-9:900, acc-7:500]
+11
View File
@@ -0,0 +1,11 @@
org.mockito.exceptions.base.MockitoException:
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()
Examples of correct argument capturing:
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());
+5
View File
@@ -0,0 +1,5 @@
receipt approved : true
then(gateway).should() : charge("acc-1", 900) ok
then(audit).should() : record("CHECKOUT", "acc-1:900") ok
shouldHaveNoMoreInteractions on audit : ok
InOrder gateway.charge -> audit.record : ok
@@ -0,0 +1,3 @@
exception : gateway unavailable
gateway.charge never called : ok
audit recorded CHECKOUT_REJECTED : ok
@@ -0,0 +1,6 @@
UnnecessaryStubbingException:
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
1. -> at lab.mockito.StrictnessTest.unusedStubIsReportedWhenTheSessionEnds(StrictnessTest.java:33)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
@@ -0,0 +1,16 @@
PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'charge' method:
paymentGateway.charge("acc-1", 1000L);
-> at lab.mockito.CheckoutService.checkout(CheckoutService.java:20)
- has following stubbing(s) with different arguments:
1. paymentGateway.charge("acc-1", 900L); stubbed with: [Returns: Receipt[id=r, account=acc-1, cents=900, approved=true]]
-> at lab.mockito.StrictnessTest.sameMethodDifferentArgumentsIsAPotentialStubbingProblem(StrictnessTest.java:48)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
- stubbing the same method multiple times using 'given().will()' or 'when().then()' API
Please use 'will().given()' or 'doReturn().when()' API for stubbing.
- stubbed method is intentionally invoked with different arguments by code under test
Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.
@@ -0,0 +1 @@
charge("acc-1", 1000) from the test file returns : null (no PotentialStubbingProblem)
+2
View File
@@ -0,0 +1,2 @@
lenient stub, mismatching call returns : null
finishMocking with unused lenient stubs : no exception
@@ -0,0 +1 @@
mismatching call on a plain mock returns : null (no strictness error)
@@ -0,0 +1,5 @@
Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3
WARNING: A Java agent has been loaded dynamically (<local-repo>/byte-buddy-agent-1.17.7.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release