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
This commit is contained in:
Abhinav Pandey 2022-11-06 15:39:05 +05:30 committed by GitHub
parent fffc0bc704
commit aeac3c1d3c
7 changed files with 174 additions and 0 deletions

View File

@ -14,4 +14,36 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
<disableXmlReport>true</disableXmlReport>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jmockit.version>1.49</jmockit.version>
</properties>
</project>

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.systemexit;
import java.io.IOException;
public class TaskDAO {
public void save(Task task) throws Exception {
}
}

View File

@ -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);
}
}
}

View File

@ -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<System>() {
@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());
}
}
}

View File

@ -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));
}
}
}

View File

@ -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);
}
}