View Javadoc
1   package org.junit.tests.running.core;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.File;
6   import java.io.InputStream;
7   
8   import org.junit.Test;
9   
10  // Make sure System.exit works as expected. We've had problems with this on some platforms.
11  public class SystemExitTest {
12  
13      private static final int EXIT_CODE = 5;
14  
15      static public class Exit {
16          public static void main(String[] args) {
17              System.exit(EXIT_CODE);
18          }
19      }
20  
21      @Test
22      public void failureCausesExitCodeOf1() throws Exception {
23          String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
24          String classPath = getClass().getClassLoader().getResource(".").getFile() + File.pathSeparator + System.getProperty("java.class.path");
25          String[] cmd = {java, "-cp", classPath, getClass().getName() + "$Exit"};
26          Process process = Runtime.getRuntime().exec(cmd);
27          InputStream input = process.getInputStream();
28          while ((input.read()) != -1) ;
29          assertEquals(EXIT_CODE, process.waitFor());
30      }
31  }