Forum Post: RE: ABL Unit testing frameworks - Comparison

Status
Not open for further replies.
A

andrew.may

Guest
I've only got experience of Java mocking. For that, we're using Mockito as our main mocking framework & like it a lot . The Mockito API makes for very nice test code & is popular enough to have been ported to a variety of different languages ). With it you can do things like: import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class MyClassTest { @Mock private MyDependentClass mockMyDependentClass; private static String REQUIRED_PARAM_VALUE = "myParam"; @Before public void setUp() throws Exception { // initialise mocking using annotations MockitoAnnotations.initMocks(this); // Setup the mock to return a value if getNumberOfThings() is called with a parameter that .equals(REQUIRED_PARAM_VALUE) when(mockMyDependentClass.getNumberOfThings(eq(REQUIRED_PARAM_VALUE))).thenReturn(1); } @Test public void testThatdoStuffIsCalledWithMyParam() { // Create an instance of MyClass which uses the mock dependency MyClass classUnderTest = new MyClass(mockMyDependentClass); // Run the method that we're testing classUnderTest.doStuff(); // Verify that the mock's method was called with a parameter that .equals(REQUIRED_PARAM_VALUE) verify(mockMyDependentClass).getNumberOfThings(eq(REQUIRED_PARAM_VALUE)); } } ... with MyClass.java being: public class MyClass { private MyDependentClass dep; MyClass(MyDependentClass dep) { super(); this.dep = dep; } public void doStuff() { // do some processing int numThings = dep.getNumberOfThings("myParam"); // do some processing using numThings } } We also use Powermock to extend Mockito for some edge cases (like mocking statics) - that's more of a "nice to have".

Continue reading...
 
Status
Not open for further replies.
Top