java - How to test methods with similar arguments in Mockito -
i'm writing junit test using mockito accepts same arguments, different sub-arguments in 1 of arguments. example:
import org.junit.before; import org.junit.beforeclass; import org.junit.test; import org.mockito.mock; import org.mockito.mockito; import org.mockito.mockitoannotations; public class samplemockito { @mock private widget widget; private static machine machine; @beforeclass public static void init() { machine = new machine(); } @before public void setup() { mockitoannotations.initmocks(this); mockito.when(widget.doaction(mockito.eq("setting 1"), mockito.any(values.class))).thenreturn("action 1 done."); mockito.when(widget.doaction(mockito.eq("setting 1"), mockito.any(values.class))).thenreturn("action 2 done."); mockito.when(widget.doaction(mockito.eq("setting 2"), mockito.any(values.class))).thenreturn("action 3 done."); mockito.when(widget.doaction(mockito.eq("setting 2"), mockito.any(values.class))).thenreturn("action 4 done."); } @test public void testperformaction() { // sample test code have not written. } }
here's pojo
public class widget { public string doaction(string param, values values) { system.out.println("do irrelevant action"); return null; } }
and worker class
import java.util.arraylist; import org.jsoup.nodes.document; public class machine { public boolean performaction(widget w, document doc) { // action 1 w.doaction("setting 1", new values("argument 1", "argument 2")); // action 2 w.doaction("setting 1", new values("argument 1a", new exception("exception message."))); // action 3 w.doaction("setting 2", new values("argument 1", "argument 2")); // action 4 w.doaction("setting 2", new values("argument 1a", new exception("exception message."))); return false; } } class values extends arraylist<object> { public values() { } public values(object... vals) { super(vals.length); (object o : vals) { add(o); } } }
as defined currently, mockito can't differentiate between actions 1 , 2, , actions 3 , 4. able tell mockito should expect number of strings, or expect exception of kind arguments of values object. there way in mockito? or other mock testing framework?
at suggestion of @jb nizet, i've changed code implement argthat(...), removed inheritence relationship between values , arraylist, , implemented this use argthat():
package tinkering; import java.util.list; import org.jsoup.nodes.document; import org.junit.before; import org.junit.beforeclass; import org.junit.test; import org.mockito.argumentmatcher; import org.mockito.mock; import org.mockito.mockito; import org.mockito.mockitoannotations; import tinkering.machine.values; public class samplemockito { @mock private widget widget; private static machine machine; private list<object> vals; @beforeclass public static void init() { machine = new machine(); } @before public void setup() { mockitoannotations.initmocks(this); } @suppresswarnings("unchecked") @test public void testperformaction() { machine.performaction(widget, new document("dumb")); mockito.verify(widget).doaction(mockito.eq("setting 1"), (values) mockito.argthat(new performactionargumentstringargsmatcher())); mockito.verify(widget).doaction(mockito.eq("setting 1"), (values) mockito.argthat(new performactionargumentmixedargsmatcher())); mockito.verify(widget).doaction(mockito.eq("setting 2"), (values) mockito.argthat(new performactionargumentstringargsmatcher())); mockito.verify(widget).doaction(mockito.eq("setting 2"), (values) mockito.argthat(new performactionargumentmixedargsmatcher())); } class performactionargumentstringargsmatcher extends argumentmatcher { @override public boolean matches(object arg) { if (arg instanceof values) { vals = ((values) arg).getlist(); if (vals.size() != 2) return false; if (!vals.get(0).equals("argument 1")) return false; if (!vals.get(1).equals("argument 2")) return false; } return true; } } class performactionargumentmixedargsmatcher extends argumentmatcher { @override public boolean matches(object arg) { if (arg instanceof values) { vals = ((values) arg).getlist(); if (vals.size() != 2) return false; if (!vals.get(0).equals("argument 1a")) return false; if (!(vals.get(1) instanceof exception) && !((exception) vals.get(1)).getmessage().equals("exception message.")) return false; } return true; } } }
changes machine.java:
package tinkering; import java.util.arraylist; import org.jsoup.nodes.document; public class machine { public boolean performaction(widget w, document doc) { // action 1 w.doaction("setting 1", new values("argument 1", "argument 2")); // action 2 w.doaction("setting 1", new values("argument 1a", new exception("exception message."))); // action 3 w.doaction("setting 2", new values("argument 1", "argument 2")); // action 4 w.doaction("setting 2", new values("argument 1a", new exception("exception message."))); return false; } class values { private arraylist<object> list; public arraylist<object> getlist() { return list; } public void setlist(arraylist<object> list) { this.list = list; } public values() { } public values(object... vals) { list = new arraylist<object>(); (object o : vals) { list.add(o); } } }
which compiles , passes.
Comments
Post a Comment