Spring Boot集成测试自动运行Spring应用程序

时间:2020-04-01 17:03:31

标签: java spring spring-boot

我正在尝试为我的应用程序编写集成测试。下面是代码:

@SpringBootTest(classes = MainApplication.class, args = "ACTIVE")
@TestPropertySource(locations = "/myproperties.properties", properties={"server.port=9010"})
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@WebAppConfiguration("src/test/resources")
@TestConfiguration
public class MainApplicationIntegrationTest {

@Autowired
private MyRepository myRepository;

@MockBean
private PureRestController controller;

@Autowired
private ApplicationContext ctx;

@Autowired
private ApplicationSummaryService summaryService;


@BeforeClass
public static void setup() {
    System.setProperty("feedtype", FeedType.ACTIVE.getValue());
}


@Test
public void runMainApplicationWithActiveFeed() throws Exception {
    CommandLineRunner runner = ctx.getBean(CommandLineRunner.class);
    Map<String,String> queryParam = new HashMap<>();
    // Auth type will never be removed
    queryParam.put("auth", "oauth");
    given(controller.getData(FeedType.ACTIVE, "",
            queryParam)).willReturn(TestingUtils.getMockPureApiResponse(TestingUtils.VALID_ACTIVE_FILE));
    runner.run("ACTIVE");
    assertThat(myRepository.count(), is(3L));
}

这里的问题是spring应用程序在开始执行runMainApplicationWithActiveFeed()函数之前启动。完成后,它将跳转到该runMainApplicationWithActiveFeed()函数并再次运行该应用程序。区别在于,在第二次运行中,它现在可以在执行测试之前模拟某些内容。

我要实现的目标是:模拟所有必要的东西,然后执行集成测试。在这里,我想避免第一次运行(因为它会自动触发)。一切都经过模拟之后,我应该可以运行我的应用程序了。

0 个答案:

没有答案