@Override public TestContext buildTestContext() { TestContext context = super.buildTestContext(); WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass()); if (webEnvironment == WebEnvironment.MOCK && hasWebEnvironmentClasses()) { context.setAttribute(ACTIVATE_SERVLET_LISTENER, true); } else if (webEnvironment != null && webEnvironment.isEmbedded()) { context.setAttribute(ACTIVATE_SERVLET_LISTENER, false); } return context; }
/** * Post process the property source properties, adding or removing elements as * required. * @param mergedConfig the merged context configuration * @param propertySourceProperties the property source properties to process */ protected void processPropertySourceProperties( MergedContextConfiguration mergedConfig, List<String> propertySourceProperties) { Class<?> testClass = mergedConfig.getTestClass(); String[] properties = getProperties(testClass); if (!ObjectUtils.isEmpty(properties)) { // Added first so that inlined properties from @TestPropertySource take // precedence propertySourceProperties.addAll(0, Arrays.asList(properties)); } if (getWebEnvironment(testClass) == WebEnvironment.RANDOM_PORT) { propertySourceProperties.add("server.port=0"); } }
/** * Return the {@link WebEnvironment} type for this test or null if undefined. * @param testClass the source test class * @return the {@link WebEnvironment} or {@code null} */ protected WebEnvironment getWebEnvironment(Class<?> testClass) { SpringBootTest annotation = getAnnotation(testClass); return (annotation == null ? null : annotation.webEnvironment()); }