您将如何进行集成测试一个spring应用程序,该应用程序是注释配置和组件扫描的,而且根本没有XML?我遇到了一个难题,那就是需要用测试组件替换生产组件,而不需要实际使用xml配置或反射注入所有@autowired部件。
示例:
interface A {...}
@Component
class AImpl implements A {
...
}
interface B {...}
@Component
class BImpl implements B {
@Autowired A a;
...
}
interface C {...}
class CI
在我的SpringBootApplication中,我有一个注入另一个可选bean的bean (如下所示)
@Service
public class A {
//B is another @Component from one of the dependencies
private Optional<B> b;
...
...
}
我正在为class A编写一个集成测试,在这里我需要@MockBean Optional<B> b。但是,由于Optional是最后一个类,所以spring会引发以下错误
不能模拟/间谍类java.util
我们有一些集成测试(使用spring框架编写)由于bean初始化异常而失败,最终导致Failed to load ApplicationContext。根据我对的理解,ApplicationContext的加载是在课堂上进行的,所以我怀疑-
一旦ApplicationContext在集成测试类运行期间由于bean初始化异常而失败(即Failed to load ApplicationContext),那么对于该特定集成测试类中存在的每个单独的集成测试,ApplicationContext是否会再次旋转(最终将失败)?
1. Asking above scenario because
我有一个小型Spring应用程序,它在"/health“处定义了一个健康端点。这是在代码中通过以下方式定义的:
@Component
public class MyHealthEndpoint implements HealthIndicator {
public Health health() {
# ...
# returns health info as JSON here
# ...
}
}
因此,在运行应用程序时,访问localhost/health的端点会返回一些应用程序健康信息。
我的问题是:测试这个问题的最好方法是什么?
我想对MyHe
我有一个实现Runnable接口的Event类。如何为这个类编写单元测试?
@Component("Event")
@RequiredArgsConstructor
@Slf4j
public class Event implements Runnable {
private final Person person;
private final Student student;
@Override
public void run() {
//somecode;
}
}
试图为Spring应用程序编写集成测试。假设我有一个A类,它包含一个B类对象。类B包含一个C类对象,我需要在这个类中模拟一个对象以进行集成测试--知道我如何做到这一点而不把每个对象作为构造函数中的参数传递吗?
例如:
@Service
Class A {
@Autowired
private B b;
public void testA() {
B.testB();
}
}
@Service
Class B {
@Autowired
private C c;
public void testB() {
我有Spring集成测试,在那里我试图伪装我的一些Beans。出于某种原因,尽管我嘲笑它们,但它们都是空的。下面是代码片段:
我想要的豆子
@Component
public class MockWS {
public String callSoapClient() throws JAXBException{
return "CallSoapCl";
}
}
使用Bean的类
public class SmDpES2PortImpl implements ES2SmDp {
@Autowired
private MockWS m