Testing console based applications/programs - Java

前端 未结 5 1452
故里飘歌
故里飘歌 2020-12-03 05:47

All,

I have written a PhoneBook application in Java that is command line based. The application basically asks for some details of user like Name, Age, Address and p

5条回答
  •  温柔的废话
    2020-12-03 06:17

    This takes a basic looping console application and makes it testable, using the ideas from oxbow_lakes' answer.

    The class-proper:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    
    public class TestableLoopingConsoleExample {
    
       public static final String INPUT_LINE_PREFIX = "> ";
       public static final String EXIT_COMMAND = "exit";
       public static final String RESPONSE_PLACEHOLDER = "...response goes here...";
       public static final String EXIT_RESPONSE = "Exiting.";
    
       public static void main(String[] cmdLineParams_ignored) throws IOException {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          PrintStream out = new PrintStream(System.out);
          PrintStream err = new PrintStream(System.err);
    
          try {
             new TestableLoopingConsoleExample().main(cmdLineParams_ignored, in, out);
          } catch (Exception e) {  //For real use, catch only the exactly expected types
             err.println(e.toString());
          }
       }
    

    ...continued...

       public void main(String[] cmdLineParams_ignored, BufferedReader in, PrintStream out)
             throws IOException {
    
          System.out.println("Enter some text, or '" + EXIT_COMMAND + "' to quit");
    
          while (true) {
    
             out.print(INPUT_LINE_PREFIX);
             String input = in.readLine();
             out.println(input);
    
             if (input.length() == EXIT_COMMAND.length() &&
                input.toLowerCase().equals(EXIT_COMMAND)) {
    
                out.println(EXIT_RESPONSE);
                return;
             }
    
             out.println(RESPONSE_PLACEHOLDER);
          }
       }
    }
    

    The test (JUnit4):

    import static org.junit.Assert.assertEquals;
    import static testableloopingconsoleapp.TestableLoopingConsoleExample.EXIT_COMMAND;
    import static testableloopingconsoleapp.TestableLoopingConsoleExample.EXIT_RESPONSE;
    import static testableloopingconsoleapp.TestableLoopingConsoleExample.INPUT_LINE_PREFIX;
    import static testableloopingconsoleapp.TestableLoopingConsoleExample.RESPONSE_PLACEHOLDER; 
    
    import org.junit.Before;
    import org.junit.Test; 
    
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.PrintStream;
    import java.io.StringReader; 
    
    public class TestableLoopingConsoleExampleTest { 
    
      private final ByteArrayOutputStream out = new ByteArrayOutputStream();
      private final ByteArrayOutputStream err = new ByteArrayOutputStream(); 
    
      @Before
      public final void resetOutputStreams() {
         out.reset();
         err.reset();
      } 
    

    ...continued...

      @Test
      public void testableMain_validInputFromString_outputAsExpected() throws Exception {
         String line1 = "input line 1\n";
         String line2 = "input line 2\n";
         String line3 = "input line 3\n";
         String exitLine = EXIT_COMMAND + "\n"; 
    
         BufferedReader in = new BufferedReader(new StringReader(
             line1 + line2 + line3 + exitLine
         ));
         String expectedOutput =
             INPUT_LINE_PREFIX + line1 +
             RESPONSE_PLACEHOLDER + "\n" +
             INPUT_LINE_PREFIX + line2 +
             RESPONSE_PLACEHOLDER + "\n" +
             INPUT_LINE_PREFIX + line3 +
             RESPONSE_PLACEHOLDER + "\n" +
             INPUT_LINE_PREFIX + exitLine +
             EXIT_RESPONSE + "\n"; 
    
         String[] ignoredCommandLineParams = null; 
    
         new TestableLoopingConsoleExample().main(ignoredCommandLineParams, in, new PrintStream(out)); 
    
         assertEquals(expectedOutput, out.toString());
      } 
    
    }
    

提交回复
热议问题