Можно ли вызвать метод юнит-теста для того же класса (по порядку и сколько раз они были вызваны)?
т.е. метод шаблона, подобный приведенному ниже:
abstract class FooBarBaz {
public abstract void foo();
public abstract void bar();
public abstract void baz();
// I want to create a unit test for this method, in the following order
public myTemplateMethod() {
foo(); // i want to check foo first;
bar(); // i want to check bar second;
baz(); // i want to check baz third;
}
}
РЕДАКТИРОВАТЬ - Реализация встроенного кода мокито
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();
}
}
РЕДАКТИРОВАТЬ - Добавлена трассировка стека
testMyTemplateMethodWithMockito(sample.FooBarBazTest) Time elapsed: 0.326 sec <<< FAILURE!
Wanted but not invoked:
fooBarBaz.foo();
-> at sample.FooBarBazTest.testMyTemplateMethodWithMockito(FooBarBazTest.java:14)
However, there were other interactions with this mock:
-> at sample.FooBarBazTest.testMyTemplateMethodWithMockito(FooBarBazTest.java:12)
at sample.FooBarBazTest.testMyTemplateMethodWithMockito(FooBarBazTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)