我是 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 Context 初始化就会失败。

感谢您的帮助。

请您参考如下方法:

只需使用您的常规 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); 
    } 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!