java - Mockito test overwritten void method -
i'm new java, unit-testing , mockito want test class.
class want test:
abstract class irc { command received(string data) { // parsing receivedcommand(prefix, command, parameters, trailing, tags); return new command(prefix, command, parameters, trailing, tags); } private void receivedcommand(string prefix, string command, string[] parameters, string trailing, map<string, string> tags) { string nick = getnickfromprefix(prefix); parsed(prefix, command, parameters, trailing); if (command.equals("mode")) { if (parameters.length == 3) { string chan = parameters[0]; string mode = parameters[1]; string name = parameters[2]; if (mode.length() == 2) { string modechar = mode.substring(1, 2); if (mode.startswith("+")) { onmodechange(chan, name, true, modechar, prefix); } else if (mode.startswith("-")) { onmodechange(chan, name, false, modechar, prefix); } } } return; } } void onmodechange(string channel, string nick, boolean modeadded, string mode, string prefix) { } }
edit: want make sure onmodechange
called after received
called.
what have far:
@test public void modechangereceivedrightsyntax() { try { irc = new irc("test") { @override public void debug(string line) { system.err.println(line); } @override void onmodechange(string channel, string nick, boolean modeadded, string mode, string prefix) { system.out.println("mode change: " + channel + " " + nick + " " + mode + " " + prefix + " " + modeadded); } }; ircmock = spy(irc); when( ircmock.received(":jtv mode #channel +o user") ).thenreturn( new command("jtv", "mode", new string[]{"#channel", "+o", "user"}, "", null) ); verify(ircmock).onmodechange("#channel", "user", true, "o", "jtv"); } catch (exception ex) { fail("exception: " + ex.tostring()); } }
the when
working verify
fails actually, there 0 interactions mock.
your code not calling business methods. mocks how received method should behave not call method. since irc class abstract, approach use spy should not mock received method tell spy delegate real method. can verify onmodechange called.
irc irc = spy(irc.class, withsettings().defaultanswer(calls_real_methods)); // business method irc.received(":jtv mode #channel +o user"); // asserts verify(irc).onmodechange("#channel", "user", true, "o", "jtv");
Comments
Post a Comment