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();
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.
source share