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

时间:2015-05-28 09:19:50

标签: spring jmockit

我是jmockit的新手,想在我的基于Java的Spring Application Configuration中模拟一个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) {
    ....
  }
}

但不幸的是,这失败了&#34; 无效的地方来应用模拟&#34;。

我想知道我是否可以在Spring Configuration类中生成jmockit模拟。我需要bean,因为它被其他bean引用,如果我不将mock作为Spring bean提供,整个Spring Context初始化都会失败。

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

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

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

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);
    }
}

答案 1 :(得分:2)

Spring-ReInject旨在用模拟取代bean。