# org.mockito.exceptions.stubbing.AnswerException: Exception thrown by answer: java.lang.RuntimeException: test exception

- **ID:** `java/mockito-answer-throws-exception`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Custom Answer implementation throws an exception when invoked during test.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 8+ | active | — | — |

## Workarounds

1. **Fix Answer implementation to handle edge cases** (90% success)
   ```
   Answer<String> answer = invocation -> {
    if (invocation.getArgument(0) == null) {
        return "default";
    }
    return invocation.getArgument(0).toString();
};
   ```
2. **Use thenAnswer with lambda** (85% success)
   ```
   when(mock.method(any())).thenAnswer(invocation -> {
    // safe logic
    return "result";
});
   ```

## Dead Ends

- **Catching exception in test and ignoring** — Hides the real issue; test may pass incorrectly. (60% fail)
- **Using thenThrow instead of custom Answer** — May not match the intended behavior if Answer has complex logic. (40% fail)
