1

I am writing Junit tests for the first time and new to the Mockito framework. How is using Mockito.mock() better than using new operator to create instances while writing Junit tests in Java?

Alex
  • 8,093
  • 6
  • 49
  • 79
Neha Arora
  • 49
  • 4
  • It's not better in general - it may be better in some circumstances. – assylias Oct 24 '16 at 10:20
  • Possible duplicate [Mockito, JUnit and Spring](http://stackoverflow.com/questions/10906945/mockito-junit-and-spring) – DimaSan Oct 24 '16 at 10:22
  • You are being downvoted because this is a broad and primarily opinion-based question. Try rephrasing by asking a more direct or specific technical question. – Alex Oct 24 '16 at 10:25
  • Please don't delete your questions after you have an answer. The intent of SO is to provide a resource for *all* developers rather than just yourself. – paxdiablo Oct 24 '16 at 12:07

2 Answers2

2

You seem to get things wrong here.

You use mocking to create "replacement objects" that you somehow pass into your code under test. Like here:

// production code
someObject.expensiveCall();

Mocking allows you to give an object to your code under test where that expensiveCall() is just a "no-op"; and on top of that: you can also use that to verify that exactly was actually made from your production code.

Thus: you are not using "mocking" in order to create objects to run tests on; their only purpose is to make testing actually possible or at least to make testing easier. See here for more details on that.

That doesn't mean that you absolutely have to use mocking all over the place. To the contrary: if you are able to write a simple test like:

@Test
public void testWhatever()  {
  assertThat(new Whatever().computeSomething(), is(some expected result));
}

that is the better way to go (your tests only deal with inputs and outputs of your "code under test"); in other words: you test contracts, without dealing with any internals of your production code.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Mockito can be used to inject dependencies that might not be there in the current testing context, not to create "dummy" objects for testing. Those mock objects are not the real thing, but "empty" versions of the mocked class, on which you can run all sorts of behaviour-simulating and result-verifying methods that are provided by Mockito.

For example following class:

class Thing {
    //Some external Dependency which provides the method 'int doWork()'
    private Dependency dependency;
    private int value;
    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }
    public int getValue() {
        return value;
    }
    // Calculates something depending on the provided amount.
    public void calculate(int amount) {
        for(int i = 0; i < amount; i++) {
            value += dependency.doWork();
        }
    }
}

Then you could do something like this in your Test:

@Test
public void should_calculate_five_times() {
    // Create mock object
    Dependency dependencyMock = Mockito.mock(Dependency.class);
    // Simulate the behaviour of the mock object
    Mockito.when(dependencyMock.doWork()).thenReturn(2);

    // Create object under test
    Thing thingToTest = new Thing();
    // Inject mock object
    thingToTest.setDependency(dependencyMock);
    // Execute method to test
    thingToTest.calculate(5);

    // Expected: dependency.doWork() should have been called 5 times
    Mockito.verify(dependencyMock, Mockito.times(5)).doWork();
    // Since dependencyMock returns 2, when doWork() is called, value should be 10
    Assert.assertEquals(10, thingToTest.getValue());
}
QBrute
  • 4,405
  • 6
  • 34
  • 40