java - Unit-testing method invocation (template method)? -
is possible unit-test method invocation on same class(in order , how many times invoked) ?
i.e. template method 1 below:
abstract class foobarbaz { public abstract void foo(); public abstract void bar(); public abstract void baz(); // want create unit test method, in following order public mytemplatemethod() { foo(); // want check foo first; bar(); // want check bar second; baz(); // want check baz third; } } edit - posted mockito code implementation
import junit.framework.testcase; import org.mockito.mockito; public class foobarbaztest extends testcase { public void testmytemplatemethodwithmockito() { foobarbaz mocked = mockito.mock(foobarbaz.class); mocked.mytemplatemethod(); mockito.verify(mocked, mockito.times(1)).foo(); mockito.verify(mocked, mockito.times(1)).bar(); mockito.verify(mocked, mockito.times(1)).baz(); } } edit - added stack trace
testmytemplatemethodwithmockito(sample.foobarbaztest) time elapsed: 0.326 sec <<< failure! wanted not invoked: foobarbaz.foo(); -> @ sample.foobarbaztest.testmytemplatemethodwithmockito(foobarbaztest.java:14) however, there other interactions mock: -> @ sample.foobarbaztest.testmytemplatemethodwithmockito(foobarbaztest.java:12) @ sample.foobarbaztest.testmytemplatemethodwithmockito(foobarbaztest.java:14) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ junit.framework.testcase.runtest(testcase.java:168) @ junit.framework.testcase.runbare(testcase.java:134) @ junit.framework.testresult$1.protect(testresult.java:110) @ junit.framework.testresult.runprotected(testresult.java:128) @ junit.framework.testresult.run(testresult.java:113) @ junit.framework.testcase.run(testcase.java:124) @ junit.framework.testsuite.runtest(testsuite.java:232) @ junit.framework.testsuite.run(testsuite.java:227) @ org.junit.internal.runners.junit38classrunner.run(junit38classrunner.java:83) @ org.apache.maven.surefire.junit4.junit4testset.execute(junit4testset.java:59) @ org.apache.maven.surefire.suite.abstractdirectorytestsuite.executetestset(abstractdirectorytestsuite.java:115) @ org.apache.maven.surefire.suite.abstractdirectorytestsuite.execute(abstractdirectorytestsuite.java:102) @ org.apache.maven.surefire.surefire.run(surefire.java:180) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.apache.maven.surefire.booter.surefirebooter.runsuitesinprocess(surefirebooter.java:350) @ org.apache.maven.surefire.booter.surefirebooter.main(surefirebooter.java:1021)
using mocking framework fine, if don't want rely upon external dependency, create test class implements abstract class. have abstract implementations of foo, bar, baz method add strings 'foo', 'bar', 'baz' shared list, in test method assert in list in correct order.
edit -- ive written tests both mockito , non-mock way. both pass--i hadn't used mockito before, might want try it....
import junit.framework.testcase; import org.junit.test; import java.util.arraylist; import java.util.list; public class foobarbaztests extends testcase { @test public void testmytemplatemethod() { list tracker = new arraylist(); simplefoobarbaz totest = new simplefoobarbaz(tracker); totest.mytemplatemethod(); assertequals("foo", tracker.get(0)); // more assertions } @test public void testmytemplatemethodwithmockito() { foobarbaz mocked = mock(foobarbaz.class); mocked.mytemplatemethod(); // times(1) unnecessary, explicit verify(mocked, times(1)).foo(); verify(mocked, times(1)).bar(); verify(mocked, times(1)).baz(); } class simplefoobarbaz extends foobarbaz { list tracker; simplefoobarbaz(list tracker) { this.tracker = tracker; } public void foo() { tracker.add("foo"); } @override public void bar() { //to change body of implemented methods use file | settings | file templates. } @override public void baz() { //to change body of implemented methods use file | settings | file templates. } // others there } }
Comments
Post a Comment