如何创建一个模拟(通过 jmockit)spring bean?

How to create a mocked (by jmockit) spring bean?

我是 jmockit 的新手,想在基于 Java 的 Spring 应用程序配置中模拟一个 bean。我以为(更希望)它会像这样:

@Configuration
public class MyApplicationConfig {

  @Bean // this bean should be a mock
  SomeService getSomeService() {
    return new MockUp<SomeService>() {@Mock String someMethod() { return ""; }}.getMockInstance();
  }

  @Bean // some other bean that depends on the mocked service bean
  MyApplication getMyApplication(SomeService someService) {
    ....
  }
}

但不幸的是,这失败了“应用模型的位置无效”。

我想知道我是否可以在 Spring 配置 类 中生成 jmockit 模拟。我需要这个 bean,因为它被其他 bean 引用,如果我不提供模拟作为 Spring bean,整个 Spring 上下文初始化就会失败。

感谢您的帮助。

Spring-ReInject 旨在用模拟替换 beans。

只需使用常规 Spring 配置即可。在测试 class 中,用 @Capturing 声明要模拟的类型。它会模拟 Spring 使用的任何实现 class。

编辑:在下面添加了完整的示例代码。

import javax.inject.*;

public final class MyApplication {
    private final String name;
    @Inject private SomeService someService;

    public MyApplication(String name) { this.name = name; }

    public String doSomething() {
        String something = someService.doSomething();
        return name + ' ' + something;
    }
}

public final class SomeService {
    public String getName() { return null; }
    public String doSomething() { throw new RuntimeException(); }
}

import org.springframework.context.annotation.*;

@Configuration
public class MyRealApplicationConfig {
    @Bean
    SomeService getSomeService() { return new SomeService(); }

    @Bean
    MyApplication getMyApplication(SomeService someService) {
        String someName = someService.getName();
        return new MyApplication(someName);
    }
}

import javax.inject.*;
import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;
import mockit.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyRealApplicationConfig.class)
public final class MyApplicationSpringTest {
    @Inject MyApplication myApplication;
    @Mocked SomeService mockService;

    @BeforeClass // runs before Spring configuration
    public static void setUpMocksForSpringConfiguration() {
        new MockUp<SomeService>() {
            @Mock String getName() { return "one"; }
        };
    }

    @Test
    public void doSomethingUsingMockedService() {
        new Expectations() {{ mockService.doSomething(); result = "two"; }};

        String result = myApplication.doSomething();

        assertEquals("one two", result);
    }
}

import org.junit.*;
import static org.junit.Assert.*;
import mockit.*;

// A simpler version of the test; no Spring.
public final class MyApplicationTest {
    @Tested MyApplication myApplication;
    @Injectable String name = "one";
    @Injectable SomeService mockService;

    @Test
    public void doSomethingUsingMockedService() {
        new Expectations() {{ mockService.doSomething(); result = "two"; }};

        String result = myApplication.doSomething();

        assertEquals("one two", result);
    }
}