Mockito; Testing an object that depends on nested dependencies (Spring)?

I'm new to using Mockito and trying to figure out a way to create a unit test class that relies on nested dependencies. What I want to do is to create a mock of the dependency objects and make the class I'm testing to use those, rather than the usual nested dependencies that will be introduced using Spring. I read the tutorials, but I'm a little confused about how to do this.

I have a class that I want to test as follows:

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 a class that I want to use as a layout (instead of the usual SubBean1 ), as shown 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 a simple test, for example:

 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 believe that there should be some pretty simple way to do this, but I don’t understand how to understand it, because I have no context to understand everything that they do / explain. If anyone could shed light on this, I would appreciate it.

+6
source share
2 answers

If you use Mockito, you create mocks by calling the Mockito static mock method. Then you can simply pass the layout to the class you are trying to test. Your installation method will 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 what you are trying to test using the static Mockito method, for example:

 when(subBeanMock.getSomething()).thenReturn("its working"); 
+8
source

In Mockito, you are not really going to create new “breadboard” implementations, but rather, you are going to mock the injection dependency interface methods by telling Mockito what to return when the method is called.

I wrote a Spring MVC Controller test using Mockito and processed it just like any other java class. I was able to mock the other other Spring beans that I had and inject those that use Spring ReflectionTestUtils to pass values ​​based on Mockito. I wrote about this in my blog in February. It has the full source of the test class and most of the source from the controller, so it may be too long to post content here.

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

+5
source

Source: https://habr.com/ru/post/886557/


All Articles