How can I mock an ES6 module import using Jest?

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy — The application code: // myModule.js import dependency from ‘./dependency’; export default (x) => { dependency.doSomething(x * 2); } And the test code: //myModule-test.js import myModule from ‘../myModule’; import dependency from … Read more

Using Mockito with multiple calls to the same method with the same arguments

Is there a way to have a stubbed method return different objects on subsequent invocations? I’d like to do this to test nondeterminate responses from an ExecutorCompletionService. i.e. to test that irrespective of the return order of the methods, the outcome remains constant. The code I’m looking to test looks something like this. // Create … Read more

Mocking static methods with Mockito

I’ve written a factory to produce java.sql.Connection objects: public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory { @Override public Connection getConnection() { try { return DriverManager.getConnection(…); } catch (SQLException e) { throw new RuntimeException(e); } } } I’d like to validate the parameters passed to DriverManager.getConnection, but I don’t know how to mock a static method. I’m using … Read more

Can Mockito capture arguments of a method called multiple times?

I have a method that gets called twice, and I want to capture the argument of the second method call. Here’s what I’ve tried: ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar).doSomething(firstFooCaptor.capture()); verify(mockBar).doSomething(secondFooCaptor.capture()); // then do some assertions on secondFooCaptor.getValue() But I get a TooManyActualInvocations Exception, as Mockito thinks that doSomething should only be … Read more

Difference between @Mock and @InjectMocks

What is the difference between @Mock and @InjectMocks in Mockito framework? 12 s 12 @Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock (or @Spy) annotations into this instance. Note you must use @RunWith(MockitoJUnitRunner.class) or Mockito.initMocks(this) to initialize these mocks and inject them … Read more

What is Mocking?

What is Mocking?                                                                                                    . 8 s 8 Prologue: If you look up the noun mock in the dictionary you will find that one of the definitions of the word is something made as an imitation. Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the … Read more

What’s the difference between faking, mocking, and stubbing?

I know how I use these terms, but I’m wondering if there are accepted definitions for faking, mocking, and stubbing for unit tests? How do you define these for your tests? Describe situations where you might use each. Here is how I use them: Fake: a class that implements an interface but contains fixed data … Read more