Description:
Summary
When using @Test(expectedExceptions = ...) and Mockito throws a MockitoException during stub setup (e.g., trying to stub a checked exception that the method no longer declares), the test hangs indefinitely instead of failing fast with a clear error.
Environment
- Java version: 17
- OS: macOS
Steps to Reproduce
- Define an interface method that does not declare a checked exception:
public interface MyService {
List<String> doWork(List<String> ids) throws JsonProcessingException;
}
- Write a test that stubs the method to throw a checked exception it doesn't declare, with
expectedExceptions set:
@Test(expectedExceptions = PaymentServiceException.class)
public void testThrowsPaymentServiceException() throws Exception {
// PaymentServiceException is a checked exception NOT in doWork's throws clause
when(myService.doWork(anyList()))
.thenThrow(mock(PaymentServiceException.class));
myService.doWork(List.of("id1", "id2"));
}
- Run the test.
Expected Behavior
The test should fail fast with a clear error, such as:
org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method!
- Or TestNG should catch the unexpected exception, report a mismatch, and move on.
Ideally, TestNG should also respect a default or configurable global timeout to prevent indefinite hangs.
Actual Behavior
The test hangs indefinitely. The Gradle test executor shows:
> :core:test > Executing test com.example.MyServiceTest
...and never progresses. The only way to recover is to manually kill the process.
Analysis
Mockito correctly detects the invalid stub (checked exception not declared by the method) and throws a MockitoException. However, the interaction between this exception and TestNG's expectedExceptions handler appears to cause a deadlock or infinite wait, rather than a clean failure.
Workaround
- Fix the stub to throw a valid exception, or
- Add
timeOut to the @Test annotation: @Test(expectedExceptions = ..., timeOut = 5000)
Suggestion
- TestNG should handle unexpected exceptions during test setup/execution gracefully, even when
expectedExceptions is specified, and fail fast rather than hang.
- Consider adding a configurable default global timeout to prevent tests from hanging indefinitely in edge cases like this.
You can open this on github.com/testng-team/testng. Just fill in your specific TestNG and Mockito versions before submitting.
Description:
Summary
When using
@Test(expectedExceptions = ...)and Mockito throws aMockitoExceptionduring stub setup (e.g., trying to stub a checked exception that the method no longer declares), the test hangs indefinitely instead of failing fast with a clear error.Environment
Steps to Reproduce
expectedExceptionsset:Expected Behavior
The test should fail fast with a clear error, such as:
org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method!Ideally, TestNG should also respect a default or configurable global timeout to prevent indefinite hangs.
Actual Behavior
The test hangs indefinitely. The Gradle test executor shows:
...and never progresses. The only way to recover is to manually kill the process.
Analysis
Mockito correctly detects the invalid stub (checked exception not declared by the method) and throws a
MockitoException. However, the interaction between this exception and TestNG'sexpectedExceptionshandler appears to cause a deadlock or infinite wait, rather than a clean failure.Workaround
timeOutto the@Testannotation:@Test(expectedExceptions = ..., timeOut = 5000)Suggestion
expectedExceptionsis specified, and fail fast rather than hang.You can open this on github.com/testng-team/testng. Just fill in your specific TestNG and Mockito versions before submitting.