gpt4 book ai didi

spring - 我正在测试的操作中没有符合条件的 bean 类型

转载 作者:行者123 更新时间:2023-12-01 22:19:29 26 4
gpt4 key购买 nike

我在 eclipse 上做一个项目,使用 tomcat、maven、spring、hibernate 和 struts。我们有 2 个应用程序:包含所有 bean(服务)和带有操作 View 等的 web 的核心。

我对服务进行了 JUnit 测试,并决定尝试对操作进行一些测试。这是我正在尝试做的一个示例:

行动

@Action(value = "/modif/register")
@ResultPath("...")
public class A{
@Autowired
private ExampleService exampleService;

public String execute(){
Example = exampleService.find(...);
...
...
}
}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class ATest extends StrutsSpringTestCase {

@Before
public void setUp(){
try {
super.setUp();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testExecute() throws Exception{
request.setParameter(...);
//filling up the request

ActionProxy proxy = super.getActionProxy("/modif/register");
A register = (A) proxy.getAction();
String result = proxy.execute();
}

}

配置

@Configuration
@ComponentScan(basePackages = {"web","core"} )
public class Config {
//configuration
}

每次我尝试启动此测试时,都会出现此错误 ActionProxy proxy = super.getActionProxy("/modif/register");

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'web.action.A': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public core.service.ExampleService web.action.A.exampleService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [core.service.ExampleService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

无论我调用哪个 bean,我都会遇到此错误。它们都在核心应用程序和我的操作中工作,我什至可以在我的测试中直接调用它们,不会出现任何错误,但每次我尝试启动测试时都会失败。

有谁知道什么可能引发此异常?

最佳答案

BeanCreationException被抛出是因为没有 ExampleService bean 在您的测试上下文中。发生这种情况的原因可能是没有为您的操作测试加载正确的上下文。

由于您使用的是 JUnit 4,而不是 StrutsSpringTestCase你应该扩展 StrutsSpringJUnit4TestCase@RunWith(SpringJUnit4ClassRunner.class) 一起玩会更好的类(class)和上下文加载。

关于spring - 我正在测试的操作中没有符合条件的 bean 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36891046/

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