when().thenThrow() do in JUnit testing?when().thenThrow() is used to tell a mock object to throw an exception when a specific method is called. It helps test how your code handles errors.
when().thenThrow() to simulate an exception?You write when(mock.method()).thenThrow(new ExceptionType()). This means when method() is called on the mock, it throws the specified exception.
Throwing exceptions in mocks lets you check if your code correctly handles errors, like catching exceptions or cleaning up resources.
readFile() to throw IOException?when(mock.readFile()).thenThrow(new IOException("File not found"));This simulates a file read error in your test.
when().thenThrow() in your test?Your test will fail with an unexpected exception unless you catch it or declare it with @Test(expected=Exception.class).
when().thenThrow() in JUnit mocking?when().thenThrow() tells the mock to throw an exception when a method is called.
thenThrow()?You can throw any exception type with thenThrow(), checked or unchecked.
The test fails if the mock does not behave as specified, including throwing exceptions.
Use @Test(expected=Exception.class) or try-catch to handle expected exceptions in tests.
when().thenThrow() syntax?when().thenThrow() is a Mockito syntax for mocking exceptions.
when().thenThrow() helps test exception handling in your code.when().thenThrow().