Files
spring-boot-rest/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSystemLambdaUnitTest.java
Abhinav Pandey a777380502 BAEL-5846 - Testing methods that call System.exit (#12952)
* BAEL-5846 - Testing methods that call System.exit

* BAEL-5846 - Testing methods that call System.exit - JMockit implementation

* BAEL-5846 - Testing methods that call System.exit - Changing test case names

* BAEL-5846 - Testing methods that call System.exit - changing test names

* BAEL-5777 - Mocking a singleton with Mockito - review incorporation
2022-11-06 15:39:05 +05:30

24 lines
808 B
Java

package com.baeldung.systemexit;
import org.junit.jupiter.api.Assertions;
import org.junit.Test;
import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
public class TaskServiceSystemLambdaUnitTest {
@Test
public void givenDAOThrowsException_whenSaveTaskIsCalled_thenSystemExitIsCalled() throws Exception {
int statusCode = catchSystemExit(() -> {
Task task = new Task("test");
TaskDAO taskDAO = mock(TaskDAO.class);
TaskService service = new TaskService(taskDAO);
doThrow(new NullPointerException()).when(taskDAO).save(task);
service.saveTask(task);
});
Assertions.assertEquals(1, statusCode);
}
}