@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext public class InferredDataSourceSqlScriptsTests { @Autowired private DataSource dataSource1; @Autowired private DataSource dataSource2;
boolean methodAnnotated = (methodAnn != null); boolean classAnnotated = (classAnn != null); MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); dirtyContext(testContext, hierarchyMode);
/** * Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass} * by dirtying the context if appropriate (i.e., according to the required mode). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredClassMode the class mode required for a context to * be marked dirty in the current phase; never {@code null} * @throws Exception allows any exception to propagate * @since 4.2 * @see #dirtyContext */ protected void beforeOrAfterTestClass(TestContext testContext, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); DirtiesContext dirtiesContext = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean classAnnotated = (dirtiesContext != null); ClassMode classMode = (classAnnotated ? dirtiesContext.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format( "%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode)); } if (classMode == requiredClassMode) { dirtyContext(testContext, dirtiesContext.hierarchyMode()); } }
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @DirtiesContext public class RewritePathGatewayFilterFactoryIntegrationTests extends BaseWebClientTests { @Test public void rewritePathFilterWorks() { testClient.get() .uri("/foo/get") .header("Host", "www.baz.org") .exchange() .expectStatus().isOk(); } @EnableAutoConfiguration @SpringBootConfiguration @Import(DefaultTestConfig.class) public static class TestConfig { } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext public class PrimaryDataSourceTests {
@RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public abstract class AbstractTransactionalAnnotatedConfigClassTests { @Autowired private Employee employee;
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @ActiveProfiles("weights-404") @DirtiesContext public class WeightRoutePredicateFactoryYaml404Tests extends BaseWebClientTests { @Autowired private WeightCalculatorWebFilter filter;
@ContextConfiguration(locations = "classpath:ctx-security-test-main.xml") @DirtiesContext @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestEmpty extends AbstractIntegrationTest { @Autowired(required=true) private MidPointPrincipalManagerMock userProfileService; @Autowired(required = true) private Clock clock;
/** * @author Radovan Semancik */ @ContextConfiguration(locations = {"classpath:ctx-model-intest-test-main.xml"}) @DirtiesContext(classMode = ClassMode.AFTER_CLASS) @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestManualCapabilities extends TestManual { @Override protected File getResourceFile() { return RESOURCE_MANUAL_CAPABILITIES_FILE; } @Override protected boolean nativeCapabilitiesEntered() { return true; } @Override protected int getNumberOfAccountAttributeDefinitions() { return 5; } }
/** * Integration tests that verify support for custom SQL script syntax * configured via {@link SqlConfig @SqlConfig}. * * @author Sam Brannen * @since 4.1 */ @ContextConfiguration(classes = EmptyDatabaseConfig.class) @DirtiesContext public class CustomScriptSyntaxSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests { @Test @Sql("schema.sql") @Sql(scripts = "data-add-users-with-custom-script-syntax.sql",// config = @SqlConfig(commentPrefix = "`", blockCommentStartDelimiter = "#$", blockCommentEndDelimiter = "$#", separator = "@@")) public void methodLevelScripts() { assertNumUsers(3); } protected void assertNumUsers(int expected) { assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); } }
@DirtiesContext public static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase { @Test public void verifyContextWasAutowired() { assertApplicationContextWasAutowired(); } }
annotations.addAnnotation( newAnnotation(constPool, DirtiesContext.class, map("classMode", enumMemberValue(constPool, ClassMode.class, dirtiesContext.classMode()))
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @DirtiesContext public class RewriteResponseHeaderGatewayFilterFactoryTests extends BaseWebClientTests { @Test public void rewriteResponseHeaderFilterWorks() { testClient.get() .uri("/headers") .header("Host", "www.rewriteresponseheader.org") .exchange() .expectStatus().isOk() .expectHeader().valueEquals("X-Request-Foo", "/42?user=ford&password=***&flag=true"); } @EnableAutoConfiguration @SpringBootConfiguration @Import(DefaultTestConfig.class) public static class TestConfig { } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/cassandra-dao-test-context.xml") @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class CassandraUserServiceImplTest extends UserServiceImplTest { }
@ContextConfiguration(locations = "classpath:ctx-repo-common-test-main.xml") @DirtiesContext @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestCommandLine extends AbstractIntegrationTest { @Autowired private CommandLineScriptExecutor commandLineScriptExecutor;
@ContextConfiguration(locations = {"classpath:ctx-model-intest-test-main.xml"}) @DirtiesContext(classMode = ClassMode.AFTER_CLASS) @Listeners({ com.evolveum.midpoint.tools.testng.AlphabeticalMethodInterceptor.class }) public class TestPersona extends AbstractPersonaTest {
/** * Transactional integration tests that verify rollback semantics for * {@link Sql @Sql} support. * * @author Sam Brannen * @since 4.1 */ @ContextConfiguration(classes = PopulatedSchemaDatabaseConfig.class) @DirtiesContext public class PopulatedSchemaTransactionalSqlScriptsTests extends AbstractTransactionalJUnit4SpringContextTests { @BeforeTransaction @AfterTransaction public void verifyPreAndPostTransactionDatabaseState() { assertNumUsers(0); } @Test @SqlGroup(@Sql("data-add-dogbert.sql")) public void methodLevelScripts() { assertNumUsers(1); } protected void assertNumUsers(int expected) { assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user")); } }
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) static class DirtiesContextDeclaredLocallyBeforeEachTestMethod { void clean() { /* no-op */ } }
/** * Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass} * by dirtying the context if appropriate (i.e., according to the required mode). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredClassMode the class mode required for a context to * be marked dirty in the current phase; never {@code null} * @throws Exception allows any exception to propagate * @since 4.2 * @see #dirtyContext */ protected void beforeOrAfterTestClass(TestContext testContext, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); DirtiesContext dirtiesContext = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean classAnnotated = (dirtiesContext != null); ClassMode classMode = (classAnnotated ? dirtiesContext.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format( "%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode)); } if (classMode == requiredClassMode) { dirtyContext(testContext, dirtiesContext.hierarchyMode()); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext public class InferredDataSourceTransactionalSqlScriptsTests { @Autowired private DataSource dataSource1; @Autowired private DataSource dataSource2;