alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (RepeatedSpringMethodRoadieTests.java)

This example Spring Framework source code file (RepeatedSpringMethodRoadieTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

after, description, exception, exception, mockcontrol, reflection, repeat, repeatedtestcase, string, test, test, timed, timed, util, verifying, verifying

The Spring Framework RepeatedSpringMethodRoadieTests.java source code

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.test.context.junit4;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.TestClass;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import org.springframework.test.annotation.Repeat;
import org.springframework.test.annotation.Timed;
import org.springframework.test.context.TestContextManager;

/**
 * Unit test for {@link SpringMethodRoadie} which focuses on proper support of
 * the {@link Repeat @Repeat} annotation.
 *
 * @author Sam Brannen
 * @since 2.5
 */
@RunWith(Parameterized.class)
public class RepeatedSpringMethodRoadieTests {

	protected final String methodName;
	protected final int expectedNumInvocations;

	protected final MockControl notifierMockControl = MockClassControl.createNiceControl(RunNotifier.class);
	protected final RunNotifier notifier = (RunNotifier) this.notifierMockControl.getMock();

	protected final MockControl descriptionMockControl = MockClassControl.createNiceControl(Description.class);
	protected final Description description = (Description) this.descriptionMockControl.getMock();

	protected final MockControl testContextManagerMockControl = MockClassControl.createNiceControl(TestContextManager.class);
	protected final TestContextManager testContextManager = (TestContextManager) this.testContextManagerMockControl.getMock();


	public RepeatedSpringMethodRoadieTests(final String methodName, final int expectedNumInvocations) throws Exception {
		this.methodName = methodName;
		this.expectedNumInvocations = expectedNumInvocations;
	}

	@Parameters
	public static Collection<Object[]> repetitionData() {
		return Arrays.asList(new Object[][] { { "testNonAnnotated", 1 }, { "testNegativeRepeatValue", 1 },
			{ "testDefaultRepeatValue", 1 }, { "testRepeatedFiveTimes", 5 } });
	}

	@Test
	public void assertRepetitions() throws Exception {

		final Class<RepeatedTestCase> clazz = RepeatedTestCase.class;
		final TestClass testClass = new TestClass(clazz);
		final RepeatedTestCase testInstance = clazz.newInstance();
		final Method method = clazz.getMethod(this.methodName, (Class[]) null);
		final SpringTestMethod testMethod = new SpringTestMethod(method, testClass);

		new SpringMethodRoadie(this.testContextManager, testInstance, testMethod, this.notifier, this.description).run();

		assertEquals("Verifying number of @Test invocations for test method [" + this.methodName + "].",
				this.expectedNumInvocations, testInstance.invocationCount);
		assertEquals("Verifying number of @Before invocations for test method [" + this.methodName + "].",
				this.expectedNumInvocations, testInstance.beforeCount);
		assertEquals("Verifying number of @After invocations for test method [" + this.methodName + "].",
				this.expectedNumInvocations, testInstance.afterCount);
	}


	protected static class RepeatedTestCase {

		int beforeCount = 0;
		int afterCount = 0;
		int invocationCount = 0;


		@Before
		protected void setUp() throws Exception {
			this.beforeCount++;
		}

		@After
		protected void tearDown() throws Exception {
			this.afterCount++;
		}

		@Test
		@Timed(millis = 10000)
		public void testNonAnnotated() {
			this.invocationCount++;
		}

		@Test
		@Repeat(-5)
		@Timed(millis = 10000)
		public void testNegativeRepeatValue() {
			this.invocationCount++;
		}

		@Test
		@Repeat
		@Timed(millis = 10000)
		public void testDefaultRepeatValue() {
			this.invocationCount++;
		}

		@Test
		@Repeat(5)
		@Timed(millis = 10000)
		public void testRepeatedFiveTimes() {
			this.invocationCount++;
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework RepeatedSpringMethodRoadieTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.