unit testing - Spock mocks showing up as nulls -
here main class:
@slf4j class consolereaderworker implements runnable { consolereader reader writer writer remotecommandselector commandselector @inject consolereaderworker(consolereader reader, writer writer, remotecommandselector commandselector) { super() this.reader = reader this.writer = writer this.commandselector = commandselector } @override void run() { try { string line while ((line = reader.readline()) != null) { commandselector.select(line).execute(writer) writer.flush() } } catch(interruptedexception ex) { log.warn("${this} interrupted with: ${exceptionutils.getstacktrace(ex)}") } } }
here attempt @ spock specification
class:
class consolereaderworkerspec extends specification { def "when enter key pressed command selected , executed , writer flushed"() { given: "a running fixture mock dependencies" consolereader reader = mock(consolereader) writer writer = mock(writer) remotecommand command = mock(remotecommand) remotecommandselector commandselector = mock(remotecommandselector) reader.readline() >> '\n' commandselector.select(_) >> command consolereaderworker worker = new consolereaderworker(reader, writer, commandselector) when: "the enter key pressed" worker.run() then: "a command executed" 1 * commandselector.select(_) } }
essentially, want confirm when run()
method invoked, commandselector#select()
method invoked any argument.
when run get:
java.lang.nullpointerexception: cannot invoke method execute() on null object @ com.nocbots.nbsvc.remote.cli.jline.consolereaderworker.run(consolereaderworker.groovy:33) @ com.nocbots.nbsvc.remote.cli.jline.consolereaderworkerspec.when enter key pressed command selected , executed , writer flushed(consolereaderworkerspec.groovy:25)
line 33 of consolereadyworker
call commandselector.select()
lives inside while
loop:
commandselector.select(line).execute(writer)
any ideas why?!? can see, i've wired commandselector#select()
mock return remotecommand
mock, should never null...ideas?
try:
then: "a command executed" 1 * commandselector.select(_) >> command
instead of:
commandselector.select(_) >> command
please have @ docs. when both mock , verify, then
block should used.
edit
this spock related error you're getting, trust me. have @ runnable script below (can run using groovy console):
@grab('org.spockframework:spock-core:1.0-groovy-2.4') @grab('cglib:cglib-nodep:3.1') import spock.lang.* class consolereaderworkerspec extends specification { def "when enter key pressed command selected , executed , writer flushed"() { given: "a running fixture mock dependencies" consolereader reader = mock(consolereader) writer writer = mock(writer) remotecommand command = mock(remotecommand) remotecommandselector commandselector = mock(remotecommandselector) reader.readline() >> '\n' consolereaderworker worker = new consolereaderworker(reader, writer, commandselector) when: "the enter key pressed" worker.run() then: "a command executed" 1 * commandselector.select(_) >> command } } class consolereaderworker implements runnable { consolereader reader writer writer remotecommandselector commandselector consolereaderworker(consolereader reader, writer writer, remotecommandselector commandselector) { this.reader = reader this.writer = writer this.commandselector = commandselector } @override void run() { try { string line while ((line = reader.readline()) != null) { commandselector.select(line).execute(writer) writer.flush() } } catch(interruptedexception ex) { log.warn("${this} interrupted with: ${exceptionutils.getstacktrace(ex)}") } } } class writer { void flush() {} } class remotecommand { void execute(writer w) { } } class consolereader { string readline() { '' } } class remotecommandselector { remotecommand select(o) { new remotecommand() } }
it fails since select
invoked twice, not fail npe. fix add following line:
reader.readline() >>> ['\n', null]
Comments
Post a Comment