【发布时间】:2023-03-15 01:14:01
【问题描述】:
我正在为我的控制器编写单元测试,但遇到了一个问题,即desktop.properties 文件在我的构建服务器上不存在并且不应该存在于那里。
我有这个主要的 SpringBoot 类:
@Configuration
@ComponentScan(basePackages="com.xxx")
@EnableJpaRepositories(basePackages = "com.xxx")
@PropertySources(value = {@PropertySource("classpath:desktop.properties")})
@EnableAutoConfiguration(exclude={JmsAutoConfiguration.class, SecurityAutoConfiguration.class, MultipartAutoConfiguration.class})
@ImportResource(value = {"classpath:multipart.xml","classpath:column-maps-config.xml","classpath:model-ui-name-maps-config.xml"})
public class ApplicationConfig extends WebMvcConfigurerAdapter implements EnvironmentAware, WebApplicationInitializer {
}
如您所见,此类导入 desktop.properties。
我有一个以以下开头的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
@WebAppConfiguration
public class SomeControllerTest {
}
如果我的环境没有desktop.properties 文件,或者我只是将其删除,则无法运行测试,因为没有依赖项就无法实例化ApplicationConfig 类。
我的问题是如何模拟 desktop.properties 或创建自定义配置以用于测试目的,以便用我的测试上下文替换 @ContextConfiguration(classes = ApplicationConfig.class)?
您能否给我一些提示?
附:当前的项目是一个相当老的旧版本,所以这只是我发现为控制器创建测试的一种方法,对pom.xml的更改最少@
【问题讨论】:
-
如果尽可能少的版本更改对您来说是一个重要的限制条件,那么提及这些版本是什么似乎可以回答您的问题。
标签: java spring spring-test spring-test-mvc