java – 模拟Spring Bean

我有以下课程:

public class Plugin {

    private DistributionManager manager;

    public void init(){
          ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        manager = context.getBean(DistributionManager.class);
    }

    public String doSomething(){
        String s =  manager.doSomething();
            return doSomethingElse(s);
    }

DistributionManager类本身具有许多自动连接的依赖项并标记为@Component

现在我想为所有这些代码运行一些单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class PluginTestCase extends  AbstractJUnit4SpringContextTests{

    @Resource
    DistributionManager manager;

    @Test
    public void testDoSomething(){
             Plugin plugin = mock(Plugin.class);

             //how can I inject DistributionMamanger bean to plugin using mockito?
             assertEquals("MyResult", plugin.doSomething());
    }

}

我之前从未使用过mockito.你能帮我模拟插件并完成单元测试吗?

更新:

我根据建议尝试以下测试:

@RunWith(MockitoJUnitRunner.class)
public class PluginTestCase {

    @Mock
    DistributionManager manager;

    @InjectMocks 
    Plugin testedPlugin;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testDao(){
        testedPlugin.init();
        testedPlugin.doSomething();
    }
}

但是,我有以下例外:

org.mockito.exceptions.base.MockitoException: Field 'testedPlugin' annotated with @InjectMocks is null.
Please make sure the instance is created *before* MockitoAnnotations.initMocks();
Example of correct usage:
   class SomeTest {
      @InjectMocks private Foo foo = new Foo();

      @Before public void setUp() {
         MockitoAnnotations.initMock(this);

    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:261)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

解决方法:

如果是想要进行单元测试的类,请不要模拟插件.这是相反的!另外,对于单元测试,我肯定会避免创建弹簧上下文,相反,您应该只进行集成测试或一些非常罕见/特定的情况.

无论如何,我想你想测试插件和管理器之间的交互.所以你一定要阅读Mockito文档,但这是第一个开始,让插件中注入了一个模拟管理器.

@RunWith(MockitoJUinitRunner.class)
public class PluginTest {
    @Mock DistributionManager mockedManager;
    @InjectMocks Plugin testedPlugin = new Plugin(); // initialization not need when using Mockito 1.9.x

    @Test public void plugin_should_call_the_the_manager_on_doSomething() {
        // given
        // when
        // then
    }

    // other scenarios
}

请注意,您只需要使用JUnit运行程序MockitoJUinitRunner.class或实用程序类和方法MockitoAnnotations.init(),但不能同时使用两者!

其他评论:

>当您使用JUnit 4.x时,您不需要通过测试开始测试方法名称,因为这些是@Test注释的,您可以将它们命名为您想要的任何可读性和表达性的测试意图.
>同样适用于设置和拆除方法,因为它们分别由@Before和@Anot注释,您可以描述您的设置或拆除.
>最后,不要将您的测试类命名为PluginTestCase,后缀TestCase仅用于抽象类,这些抽象类将通过Test后缀的实际测试(如MyClassTest)进行扩展.无论如何,Maven Surefire将寻找名为* Test的类.

上一篇:如何使用Mockito模拟For循环


下一篇:java – 使用Mockito注入autowired bean并在mock上设置一些属性