title: JUnit testing of OutputStream
body: <p>I am trying to write a test for the writeMessage() method. But I have no idea how to start, since I need to test an OutputStream. This should be something like a small chat. It should read a message from console, write it to the text file and than print all messages that have been written to the file. </p>
  
  <p>This is for a university project.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintStream;
  
  public class ChatIntImplement implements ChatI {
  
      public static String readMessage() throws IOException, NullPointerException{
          InputStream is = System.in;
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String message = null;
  
          try {
              message = br.readLine();
  
          }catch(IOException ex) {
              System.err.println("couldn't write data (fatal)");
              System.exit(0);
          }
          return message;
      }
  
      public static void messageToFile(String message) throws IOException {
          try {
          String filename = "savedMessage.txt";
          OutputStream os = new FileOutputStream(filename, true);
          PrintStream ps = new PrintStream(os);
          ps.println(message);
          }catch (FileNotFoundException ex) {
              System.err.println("couldn't open file - fatal");
              System.exit(0);
          }
      }
  
      public static void showMessages() {
          InputStream is = null;
          try {
          is = new FileInputStream("savedMessage.txt");
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String message;
          while((message = br.readLine())!= null) {
              System.out.println(message);
          }
          }catch(FileNotFoundException ex) {
              System.err.println("couln't open file -fatal");
              System.exit(0);
          }catch(IOException e) {
              System.err.println("couldn't read data (fatal)");
              System.exit(0);
          }
  
      }
  
      @Override
      public void writeMessage(String message){
          try {
          messageToFile(message);
          showMessages();
          }catch (IOException e) {
              System.err.println("couldn't write data (fatal");
              System.exit(0);
          }
      }
  
      @Override
      public void exit() {
          // TODO Auto-generated method stub
  
      }
  
      public static void main(String[] args) {
          ChatIntImplement chat = new ChatIntImplement();
          try {
          String message = readMessage();
          chat.writeMessage(message);
          }catch (IOException e) {
              System.err.println("couldn't write data (fatal");
              System.exit(0);
          }catch (NullPointerException npe) {
              System.err.println("Du hast nichts eingegeben");
              System.exit(0);
          }
  
      }
  
  
      }
  </code></pre>