diff --git a/testing-modules/junit-5-advanced/pom.xml b/testing-modules/junit-5-advanced/pom.xml index 23b52fc9c4..ae6fd1559e 100644 --- a/testing-modules/junit-5-advanced/pom.xml +++ b/testing-modules/junit-5-advanced/pom.xml @@ -14,4 +14,36 @@ 1.0.0-SNAPSHOT + + + com.github.stefanbirkner + system-lambda + 1.2.1 + test + + + org.jmockit + jmockit + ${jmockit.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar + + true + + + + + + 1.49 + \ No newline at end of file diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/Task.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/Task.java new file mode 100644 index 0000000000..06fd43ee5a --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/Task.java @@ -0,0 +1,17 @@ +package com.baeldung.systemexit; + +public class Task { + private String name; + + public Task(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskDAO.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskDAO.java new file mode 100644 index 0000000000..85415336b6 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskDAO.java @@ -0,0 +1,8 @@ +package com.baeldung.systemexit; + +import java.io.IOException; + +public class TaskDAO { + public void save(Task task) throws Exception { + } +} diff --git a/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskService.java b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskService.java new file mode 100644 index 0000000000..8f15781a11 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/main/java/com/baeldung/systemexit/TaskService.java @@ -0,0 +1,18 @@ +package com.baeldung.systemexit; + +public class TaskService { + + private final TaskDAO taskDAO; + + public TaskService(TaskDAO taskDAO) { + this.taskDAO = taskDAO; + } + + public void saveTask(Task task) { + try { + taskDAO.save(task); + } catch (Exception e) { + System.exit(1); + } + } +} \ No newline at end of file diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceJMockitUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceJMockitUnitTest.java new file mode 100644 index 0000000000..d1dd6956fc --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceJMockitUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.systemexit; + +import mockit.Mock; +import mockit.MockUp; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; + +public class TaskServiceJMockitUnitTest { + + @Test + public void givenDAOThrowsException_whenSaveTaskIsCalled_thenSystemExitIsCalled() throws Exception { + + new MockUp() { + @Mock + public void exit(int value) { + throw new RuntimeException(String.valueOf(value)); + } + }; + + Task task = new Task("test"); + TaskDAO taskDAO = mock(TaskDAO.class); + TaskService service = new TaskService(taskDAO); + try { + doThrow(new NullPointerException()).when(taskDAO).save(task); + service.saveTask(task); + } catch (RuntimeException runtimeException) { + Assertions.assertEquals("1", runtimeException.getMessage()); + } + } +} diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSecurityManagerUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSecurityManagerUnitTest.java new file mode 100644 index 0000000000..c774f94ca5 --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSecurityManagerUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.systemexit; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.security.Permission; + +import static org.mockito.Mockito.*; + +class TaskServiceSecurityManagerUnitTest { + + @BeforeEach + void setUp() { + System.setSecurityManager(new NoExitSecurityManager()); + } + + @Test + void givenDAOThrowsException_whenSaveTaskIsCalled_thenSystemExitIsCalled() throws Exception { + Task task = new Task("test"); + TaskDAO taskDAO = mock(TaskDAO.class); + TaskService service = new TaskService(taskDAO); + try { + doThrow(new NullPointerException()).when(taskDAO).save(task); + service.saveTask(task); + } catch (RuntimeException e) { + Assertions.assertEquals("1", e.getMessage()); + } + } + + private static class NoExitSecurityManager extends SecurityManager { + @Override + public void checkPermission(Permission perm) { + } + + @Override + public void checkExit(int status) { + super.checkExit(status); + throw new RuntimeException(String.valueOf(status)); + } + } +} \ No newline at end of file diff --git a/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSystemLambdaUnitTest.java b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSystemLambdaUnitTest.java new file mode 100644 index 0000000000..4cdcb5470f --- /dev/null +++ b/testing-modules/junit-5-advanced/src/test/java/com/baeldung/systemexit/TaskServiceSystemLambdaUnitTest.java @@ -0,0 +1,23 @@ +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); + } +}