我正在为我的控制器编写单元测试,并面临一个问题,即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)我的测试上下文?
你能不能给我一些提示?
PS 当前项目是一个相当老的项目,有旧版本,所以这只是我发现的一种方法,可以为控制器创建测试,而对控制器的更改最少 pom.xml
MM们
慕婉清6462132
手掌心
相关分类