Gor*_*666 56 spring spring-boot
如果在TestCase类中有这样的注释:
@SpringApplicationConfiguration(classes = {Application.class})
这将导致Application.class实现CommandLineRunner接口运行所需的方法
public void run(String... args) throws Exception
我仍然认为这主要是一种不想要的行为,因为在您的测试环境中,您可能不想启动整个应用程序.
我想到了解决这个问题的两个解决方案:
CommandLineRunner从我的Application班级中删除界面这个解决方案都需要大量的编码.你有更方便的解决方案吗?
Mic*_*ore 45
Jan的解决方案可以更容易实现.
在您的测试类中,激活"test"配置文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
public class MyFancyTest {}
在CommandLineRunner中,将配置文件设置为NOT test:
@Component
@Profile("!test")
public class JobCommandLineRunner implements CommandLineRunner {}
然后,您不必在应用程序中手动设置配置文件.
jmg*_*net 23
如Spring文档http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html中所述,您可以将@ContextConfiguration与特殊初始值设定项一起使用:
ConfigFileApplicationContextInitializer是一个ApplicationContextInitializer,可以应用于您的测试以加载Spring Boot application.properties文件.当您不需要@SpringApplicationConfiguration提供的完整功能时,可以使用此功能.
在此示例中,将初始化anyComponent并注入属性,但不会执行run(args)方法.(Application.class是我的主要春季入口点)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, 
                      initializers = ConfigFileApplicationContextInitializer.class)
public class ExtractorTest {
    @Autowired
    AnyComponent anyComponent;
    @Test
    public void testAnyComponent() {
       anyComponent.anyMethod(anyArgument);
    }
}
Bog*_*mac 21
您可以在与应用程序相同的包中定义一个看起来完全相同的测试配置,除了它排除了实现CommandLineRunner的bean.这里的关键是@ ComponentScan.excludeFilters:
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
@EnableAutoConfiguration
public class TestApplicationConfiguration {
}
然后,只需替换测试中的配置:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplicationConfiguration.class)
public class SomeApplicationTest {
    ...
}
现在不会执行CommandLineRunner,因为它们不是配置的一部分.
jih*_*hor 16
我参加派对有点晚了,但合理的做法是用豆子标记@ConditionalOnProperty,例如
@ConditionalOnProperty(prefix = "job.autorun", name = "enabled", havingValue = "true", matchIfMissing = true)
public CommandLineRunner myRunner() {...}
以下注释将在测试中禁用它:
@SpringBootTest(properties = {"job.autorun.enabled=false"})
小智 6
如果您安装了模拟框架(例如 MockMVC),您可以创建 CommandLineRunner 实现的模拟实例,或多或少地禁用它:
@MockBean 私有 TextProcessor myProcessor;
小智 2
我通过不实现 CommandLineRunner 解决了这个问题。只需从上下文中获取一个 bean,然后调用它的方法并传递 argv 即可。这样您将获得相同的结果,并且应用程序在运行测试时不会自动启动。
| 归档时间: | 
 | 
| 查看次数: | 17311 次 | 
| 最近记录: |