cod*_*ane 5 unit-testing spring-boot
我有一个 Spring Boot 命令行应用程序:
@SpringBootApplication
public class TheApplication implements CommandLineRunner {
public void run(String... args) throws Exception {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试@Service该TheApplication用途。通常,如果它是一个TheApplication没有任何功能的 mvc 应用程序,这很好,但在这种情况下,它是 aCommandLineRunner并且每次我想测试 a 时@Service,run都会调用 , 从而导致测试出现问题。我需要标注测试的@Service用途@Value来配置本身,而是这些注解导致启动应用程序和run方法被调用。
有没有办法运行弹簧启动测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class AccessTokenTest {
...
}
Run Code Online (Sandbox Code Playgroud)
不TheApplication::run被调用?
cod*_*ane 11
根据这个答案,结果很容易。
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class))
@EnableAutoConfiguration
public class TestApplicationConfiguration {
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplicationConfiguration.class)
public class TheTest {
@Autowired
TheService theService;
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7105 次 |
| 最近记录: |