org.springframework.test.annotation.DirtiesContext.MethodMode Java Examples
The following examples show how to use
org.springframework.test.annotation.DirtiesContext.MethodMode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractDirtiesContextTestExecutionListener.java From spring-analysis-note with MIT License | 5 votes |
/** * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod} * by dirtying the context if appropriate (i.e., according to the required modes). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredMethodMode the method mode required for a context to * be marked dirty in the current phase; 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 beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Method testMethod = testContext.getTestMethod(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class); DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean methodAnnotated = (methodAnn != null); boolean classAnnotated = (classAnn != null); MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] " + "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode, methodAnnotated, methodMode)); } if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) { HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); dirtyContext(testContext, hierarchyMode); } }
Example #2
Source File: AbstractDirtiesContextTestExecutionListener.java From java-technology-stack with MIT License | 5 votes |
/** * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod} * by dirtying the context if appropriate (i.e., according to the required modes). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredMethodMode the method mode required for a context to * be marked dirty in the current phase; 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 beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Method testMethod = testContext.getTestMethod(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class); DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean methodAnnotated = (methodAnn != null); boolean classAnnotated = (classAnn != null); MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] " + "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode, methodAnnotated, methodMode)); } if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) { HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); dirtyContext(testContext, hierarchyMode); } }
Example #3
Source File: AbstractDirtiesContextTestExecutionListener.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod} * by dirtying the context if appropriate (i.e., according to the required modes). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredMethodMode the method mode required for a context to * be marked dirty in the current phase; 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 beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Method testMethod = testContext.getTestMethod(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class); DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean methodAnnotated = (methodAnn != null); boolean classAnnotated = (classAnn != null); MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] " + "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode, methodAnnotated, methodMode)); } if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) { HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); dirtyContext(testContext, hierarchyMode); } }
Example #4
Source File: DirtiesContextIntegrationTest.java From tutorials with MIT License | 5 votes |
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) @Test @Order(3) void addJohnDoeAndPrintCache() { userCache.addUser("John Doe"); userCache.printUserList("addJohnDoeAndPrintCache"); }
Example #5
Source File: AdHocSchedulerParamsTest.java From spring-batch-rest with Apache License 2.0 | 4 votes |
@Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void paramsAddedToScheduledJobWorks() throws InterruptedException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, SchedulerException { Job job1 = job("j1"); Job job2 = job("j2"); jobBuilder.registerJob(job1); jobBuilder.registerJob(job2); Map<String, Object> params = new HashMap<String, Object>(); params.put("testParamKey", "testParamValue"); JobParameters expectedParams = JobParamUtil.convertRawToJobParams(params); JobConfig job1Config = JobConfig.builder().name("j1").properties(params).build(); JobConfig job2Config = JobConfig.builder().name("j2").properties(params).build(); Date now = Date.from(Instant.now().plusMillis(2000)); scheduler.start(); when(jobLauncher.run(job1, expectedParams)) .thenReturn(new JobExecution(new Random().nextLong(), expectedParams)); when(jobLauncher.run(job2, expectedParams)) .thenReturn(new JobExecution(new Random().nextLong(), expectedParams)); scheduler.schedule(job1Config, now); scheduler.schedule(job2Config, TRIGGER_EVERY_SECOND); ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class); ArgumentCaptor<JobParameters> jobParamCaptor = ArgumentCaptor.forClass(JobParameters.class); verify(jobLauncher, timeout(8000).times(2)).run(jobCaptor.capture(), jobParamCaptor.capture()); List<JobParameters> paramsListAfterCall = jobParamCaptor.getAllValues(); Assertions.assertEquals(paramsListAfterCall.size(), 2); for (JobParameters jobParams : paramsListAfterCall) { Assertions.assertEquals(jobParams.getString("testParamKey"), "testParamValue"); } scheduler.pause(); }