Incomplete Mockito Stub

ID

java.mockito_stubbing_chains_complete

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

mockito, reliability, testing

Description

Reports a Mockito stub configuration left syntactically incomplete: a when(mock.method(…​)) with nothing chained onto it, or a doReturn/doThrow/doAnswer/doNothing/doCallRealMethod call missing its trailing .when(mock).method(…​).

Rationale

Both halves of a Mockito stub — which method to intercept, and what it should do — only take effect once the chain is complete. Stop halfway and the configuration is silently dropped: the method keeps returning its default value, and the test can pass or fail for reasons that have nothing to do with the behavior it was written to check.

PaymentGateway gateway = mock(PaymentGateway.class);
when(gateway.charge(500)); // nothing is ever configured — charge() still returns null

Remediation

Complete the chain with a behavior method, or with the method to stub.

PaymentGateway gateway = mock(PaymentGateway.class);
when(gateway.charge(500)).thenReturn(new Receipt());

What is not reported

The result being handed on rather than discarded — assigned to a variable, stored via a plain assignment, returned from a method, chosen by a ternary, passed as an argument to another call, or produced as an expression lambda’s result — is left alone: that is the shape of a helper that hands the incomplete stub onward to be finished elsewhere.

© 2026 Xygeni. All rights reserved