Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito, Testing an object that relies on injected dependencies (Spring)?

I'm new to using Mockito and am trying to understand a way to make a unit test of a class that relies on injected dependencies. What I want to do is to create mock objects of the dependencies and make the class that I am testing use those instead of the regular injected dependencies that would be injected by Spring. I have been reading tutorials but am a bit confused on how to do this.

I have one the class I want to test like this:

package org.rd.server.beans;

import org.springframework.beans.factory.annotation.Autowired;

public class TestBean1 {

    @Autowired
    private SubBean1 subBean1;

    private String helloString;

    public String testReturn () {

        subBean1.setSomething("its working");
        String something = subBean1.getSomething();
        helloString = "Hello...... " + something;

        return helloString;
    }

Then I have the class that I want to use as a mock object (rather than the regular SubBean1 class, like below:

package org.rd.server.beans.mock;

public class SubBean1Mock {

    private String something;

    public String getSomething() {
        return something;
    }

    public void setSomething(String something) {
        this.something = something;
    }


}


    }

I just want to try running a simple test like this:

package test.rd.beans;
import org.rd.server.beans.TestBean1;

import junit.framework.*;


public class TestBean1Test extends TestCase
{
    private TestBean1 testBean1;

    public TestBean1Test(String name)
    {
        super(name);
    }

    public void setUp()
    {
        testBean1 = new TestBean1();
        // Somehow inject the mock dependency SubBean1Mock ???

    }

    public void test1() {
        assertEquals(testBean1.testReturn(),"working");
    }
}

I figure there must be some fairly simple way to do this but I can't seem to understand the tutorials as I don't have the context yet to understand everything they are doing / explaining. If anyone could shed some light on this I would appreciate it.

like image 496
Rick Avatar asked Apr 24 '11 00:04

Rick


People also ask

Is Mockito a dependency injection?

Mockito tries to inject mocked dependencies using one of the three approaches, in the specified order. Constructor Based Injection - when there is a constructor defined for the class, Mockito tries to inject dependencies using the biggest constructor.

What is an inject mock in Mockito?

@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 (JUnit 4).

How is Mockito used with spring?

Summary. Mocking in unit testing is extensively used in Enterprise Application Development with Spring. By using Mockito, you can replace the @Autowired components in the class you want to test with mock objects. You will be unit testing controllers by injecting mock services.

What is Mockito in testing?

Mockito is a java based mocking framework, used in conjunction with other testing frameworks such as JUnit and TestNG. It internally uses Java Reflection API and allows to create objects of a service. A mock object returns a dummy data and avoids external dependencies.


2 Answers

If you're using Mockito you create mocks by calling Mockito's static mock method. You can then just pass in the mock to the class you're trying to test. Your setup method would look something like this:

testBean1 = new TestBean1();
SubBean1 subBeanMock = mock(SubBean1.class);
testBean1.setSubBean(subBeanMock);

You can then add the appropriate behavior to your mock objects for whatever you're trying to test with Mockito's static when method, for example:

when(subBeanMock.getSomething()).thenReturn("its working");
like image 118
Ed Kurowski Avatar answered Oct 07 '22 17:10

Ed Kurowski


In Mockito you aren't really going to create new "mock" implementations, but rather you are going to mock out the methods on the interface of the injected dependency by telling Mockito what to return when the method is called.

I wrote a test of a Spring MVC Controller using Mockito and treated it just like any other java class. I was able to mock out the various other Spring beans I had and inject those using Spring's ReflectionTestUtils to pass in the Mockito based values. I wrote about it in my blog back in February. It has the full source for the test class and most of the source from the controller, so it's probably too long to put the contents here.

http://digitaljoel.nerd-herders.com/2011/02/05/mock-testing-spring-mvc-controller/

like image 23
digitaljoel Avatar answered Oct 07 '22 17:10

digitaljoel