Created
December 3, 2013 20:03
-
-
Save meaganewaller/7776518 to your computer and use it in GitHub Desktop.
java mocking paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mastermind; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.io.PrintStream; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class CommandLineInterface { | |
| PrintStream output = System.out; | |
| public void setOutput(PrintStream output) { | |
| this.output = output; | |
| } | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CommandLineInterfaceTest { | |
| private CommandLineInterface cli = new CommandLineInterface(); | |
| .. | |
| @Test | |
| public void itDisplaysAWelcomeMessage() { | |
| MockOutputStream out = new MockOutputStream(); | |
| PrintStream printStream = new PrintStream(out); | |
| cli.setOutput(printStream); | |
| cli.displayWelcomeMessage(); | |
| assertEquals("", out.getStringHistory()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The MockOutputStream does work, it can mocks the output, when I remove line 8 from the CommandLineInterface test, the output is printed to the console. However, the assertion is failing with | |
| Expected: | |
| Actual : [] | |
| So, the StringHistory array isn't getting populated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mastermind; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.util.ArrayList; | |
| public class MockOutputStream extends OutputStream { | |
| ArrayList<String> stringHistory = new ArrayList<String>(); | |
| @Override | |
| public void write(int b) throws IOException { | |
| } | |
| public void write(String message) throws IOException { | |
| stringHistory.add(message); | |
| } | |
| public ArrayList<String> getStringHistory() { | |
| System.out.println("hi"); | |
| return stringHistory; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment