gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:31:54 26 4
gpt4 key购买 nike

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

关于spring - 如何创建一个模拟(通过 jmockit)的 spring bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com