Skip to content

Commit

Permalink
add interaction test with conditions on the mock method's argument: l…
Browse files Browse the repository at this point in the history
…ist command must print text that contains all available elements
  • Loading branch information
Michel Vollebregt committed Apr 25, 2011
1 parent 39c16ab commit d1e322c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
22 changes: 21 additions & 1 deletion mockitotest/com/vxcompany/spocksnippets/TextConsoleTest.java
Expand Up @@ -17,13 +17,19 @@

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.io.PrintStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static com.vxcompany.spocksnippets.TestUtil.*;

import static org.junit.Assert.assertTrue;
import static org.mockito.AdditionalMatchers.and;
import static org.mockito.AdditionalMatchers.find;
import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

Expand All @@ -34,11 +40,13 @@ public class TextConsoleTest {

private TextConsole console;
private Game game;
private PrintStream output;

@Before
public void setUp() {
game = mock(Game.class);
console = new TextConsole(game);
output = mock(PrintStream.class);
console = new TextConsole(game, output);
}

@Test
Expand All @@ -47,4 +55,16 @@ public void textConsole_commandForCombiningTwoAvailableElements_callGameCombine(
console.eval("combine first with second");
verify(game).combine(new Element("first"), new Element("second"));
}

@Test
public void textConsole_listCommand_printAvailableElements() {
game.setAvailableElements(createSet(new Element("first"), new Element("second"), new Element("third")));
console.eval("list");
// for easier test cases we could use argument matchers, but because we have to check
// three boolean expressions on the argument, we have to use the more complicated ArgumentCaptor
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(output).println(captor.capture());
String text = captor.getValue();
assertTrue("list command should print available elements", text.contains("first") && text.contains("second") && text.contains("third"));
}
}
12 changes: 10 additions & 2 deletions spocktest/com/vxcompany/spocksnippets/TextConsoleSpec.groovy
Expand Up @@ -26,10 +26,12 @@ class TextConsoleSpec extends Specification {

def console
def game
def output

def setup() {
game = Mock(Game)
console = new TextConsole(game)
output = Mock(PrintStream)
console = new TextConsole(game, output)
}

def "on text console, command for combining two available elements should call Game.combine"() {
Expand All @@ -40,6 +42,12 @@ class TextConsoleSpec extends Specification {
1 * game.combine(new Element("first"), new Element("second"))
}


def "on text console, list command prints available elements"() {
when:
game.availableElements() >> [new Element("first"), new Element("second"), new Element("third")]
console.eval("list")
then:
1 * output.println({ it.contains("first") && it.contains("second") && it.contains("third") })
}

}
5 changes: 4 additions & 1 deletion src/com/vxcompany/spocksnippets/TextConsole.java
Expand Up @@ -16,6 +16,7 @@
// along with SpockSnippets. If not, see <http://www.gnu.org/licenses/>.

import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
Expand All @@ -24,9 +25,11 @@
public class TextConsole {

private Game game;
private PrintStream output;

public TextConsole(Game game) {
public TextConsole(Game game, PrintStream output) {
this.game = game;
this.output = output;
}

public void eval(String userCommand) {
Expand Down

0 comments on commit d1e322c

Please sign in to comment.