Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 为什么从SpringBoot应用程序中的测试中删除WebMvcTest注释会导致ComponentScan失败?_Spring Mvc_Spring Boot_Spring Test_Spring Test Mvc - Fatal编程技术网

Spring mvc 为什么从SpringBoot应用程序中的测试中删除WebMvcTest注释会导致ComponentScan失败?

Spring mvc 为什么从SpringBoot应用程序中的测试中删除WebMvcTest注释会导致ComponentScan失败?,spring-mvc,spring-boot,spring-test,spring-test-mvc,Spring Mvc,Spring Boot,Spring Test,Spring Test Mvc,我有一个测试定义为: @ComponentScan(basePackages = { ... }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { AppConfig.class }) @WebMvcTest(secure = false) public class MyTest extends AbstractJUnit4SpringContextTests { @Autowire

我有一个测试定义为:

@ComponentScan(basePackages = { ... })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class })
@WebMvcTest(secure = false)
public class MyTest extends AbstractJUnit4SpringContextTests  {

     @Autowired SomeClass target;

     @Test public void test() { Assert.assertTrue(target != null); } // MCVE

}
以下是我的ContextConfiguration类:

@Configuration
public class AppConfig {

    @Bean
    @ConfigurationProperties(prefix = "datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }

}
此测试工作正常,但由于@WebMvcTest注释,它在MVC初始化过程中浪费了大量时间。此特定测试不需要任何MVC功能。为了让@Autowired注释正常工作,经过反复试验,注释最终出现在那里。但现在我想删除它

所以。。。我继续并删除了@WebMvcTest注释。但是测试失败了,因为显然@ComponentScan没有任何效果,除非@WebMvcTest存在,我现在记得,这就是我添加它的原因

以下是实际的错误消息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '...' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我试着添加@SpringBootTest,但没什么不同

如果改为添加@EnableAutoConfiguration,则会出现不同的错误:

java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
我缺少什么注释?

编辑:

为什么从SpringBoot应用程序中的测试中删除WebMvcTest注释会导致ComponentScan失败

因为您没有以Spring的IoC/依赖项注入能够正确工作的方式对类进行注释

现在答案已经很清楚了,这里有一些建议供您尝试。它们可能工作,也可能不工作,这取决于您的项目和依赖项等的组合方式

结束编辑:

此特定测试不需要任何MVC功能

你应该考虑使用一个更直接的注释来测试你的测试,如果你不需要所有的MVC,但是你需要自动配置。例如,您似乎正在使用某些数据系统进行测试。也许or注释将提供一种更为精简的方法。这些将启用自动配置IoC和依赖项注入

我遗漏了什么注解

如上所述,您缺少的注释是@Configuration

这与直觉相反,因为这不是一个配置类。但是,如果没有适当的注释来实现依赖项注入,这是实现这一功能的唯一方法。为什么?

从文档中:

公共@接口组件扫描

  Configures component scanning directives for use with @Configuration classes.
因此,您的选择是:

如上所述,使用不同的自动配置注释 添加@Configuration注释
您可能希望添加测试的其余部分,或者添加一些关于它的功能的信息。这种设置看起来很奇怪。什么是spring启动版本,什么是contextconfiguration加载,什么是组件扫描?是否存在与加载此文件相关联的堆栈跟踪?还有,你的主要应用程序是什么?由于webmvctest是一个切片测试,它也将加载在主引导应用程序上定义的任何内容,例如,如果您碰巧在SpringBootApplication类上定义了任何Bean或@Enable,它们也将被加载,但这一点值得怀疑,因为您尝试了SpringBootTestSpringBoot is 2.0.5.RELEASE。ContextConfiguration正在加载JdbcTemplate。组件扫描的是SomeClass所在的包。为什么主应用程序中的内容很重要?我正在尝试仅对某个类设置一个简单的单元测试。我将添加contextconfiguration类。请看这里:JdbcTest为我提供了无法用嵌入式数据库替换数据源的测试。。“配置”给出了原始错误,表明componentscan不起作用。@AlexR-非常欢迎您的回答,我很高兴花这么多时间帮助您。我对答案进行了一些编辑,以澄清答案是明确提供的。我不知道你的项目是什么样子。如果你想让这个项目在GitHub上可用,我会看一看并为你修复它。但我猜不出你在黑暗中隐藏着什么:-。谢谢你的努力。在这一点上,我认为最好等一等,看看是否还有其他想法。也许经历过完全相同问题的人可以毫不费力地回忆起解决方案。我已经试过了你建议的每一种组合,没有任何效果。