CVS: junit/src/org/junit/tests CustomMethodRunnerTest.java, NONE, 1.1 AllTests.java, 1.4, 1.5

David Saff <[email protected]>
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Update of /cvsroot/junit/junit/src/org/junit/tests
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv23274/src/org/junit/tests

Modified Files:
	AllTests.java 
Added Files:
	CustomMethodRunnerTest.java 
Log Message:
Another potential enhancement.  RunMethodWith allows individual test methods to be run with different runners.

--- NEW FILE: CustomMethodRunnerTest.java ---
package org.junit.tests;

import static org.junit.Assert.*;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.junit.Assert;
import org.junit.Test;
import org.junit.internal.runners.TestIntrospector;
import org.junit.internal.runners.TestMethod;
import org.junit.internal.runners.TestMethodRunner;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.MethodRunner;
import org.junit.runners.Replaces;
import org.junit.runners.RunMethodWith;

public class CustomMethodRunnerTest {
	@RunMethodWith(CustomMethodRunner.class)
	@Replaces(Test.class)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface MyIgnore {

	}

	public static class CustomMethodRunner implements MethodRunner {
		public void run(TestMethod method, RunNotifier notifier) {
			notifier.fireTestStarted(method.getDescription());
			notifier.fireTestIgnored(method.getDescription());
			notifier.fireTestFinished(method.getDescription());
		}
	}

	public static class UsesCustomMethodRunner {
		@MyIgnore
		public void thisIsIgnored() {
			Assert.fail();
		}
	}

	@Test
	public void countCustomTests() {
		assertEquals(1, new TestIntrospector(UsesCustomMethodRunner.class)
				.getTestMethods(Test.class).size());
	}

	@Test
	public void customMethodRunner() {
		Result result= JUnitCore.runClasses(UsesCustomMethodRunner.class);
		assertEquals(1, result.getIgnoreCount());
		assertEquals(0, result.getFailureCount());
	}

	public static class EvilCustomMethodRunner implements MethodRunner {
		public EvilCustomMethodRunner() {
			throw new RuntimeException();
		}

		public void run(TestMethod method, RunNotifier notifier) {
			// do nothing
		}
	}

	public static class UsesEvilCustomMethodRunner {
		@RunMethodWith(EvilCustomMethodRunner.class)
		@Test
		public void thisShouldNeverRun() {
		}
	}

	@Test
	public void problemInstantiatingCustomRunner() {
		Result result= JUnitCore.runClasses(UsesEvilCustomMethodRunner.class);
		Failure failure= result.getFailures().get(0);
		assertTrue(failure.getMessage().contains(
				"Exception creating custom method runner"));
	}

	public static class PointlessCustomMethodRunner implements MethodRunner {
		public void run(TestMethod method, RunNotifier notifier) {
			new TestMethodRunner(method.getTest(), method.getJavaMethod(),
					notifier, method.getDescription()).run();
		}
	}

	public static class UsesPointlessCustomMethodRunner {
		@RunMethodWith(PointlessCustomMethodRunner.class)
		@Test
		public void thisShouldRun() {
			fail("Something happened!");
		}
	}

	@Test
	public void allInformationIsPassedToMethodRunner() {
		Result result= JUnitCore
				.runClasses(UsesPointlessCustomMethodRunner.class);
		Failure failure= result.getFailures().get(0);
		assertEquals("Something happened!", failure.getMessage());
	}

	@Test
	public void allInformationIsPassedToNotifier() {
		RunListener listener= new RunListener() {
			@Override
			public void testStarted(Description description) throws Exception {
				assertEquals(
						Description.createTestDescription(
								UsesPointlessCustomMethodRunner.class,
								"thisShouldRun"), description);
			}
		};
		JUnitCore core= new JUnitCore();
		core.addListener(listener);
		core.run(UsesPointlessCustomMethodRunner.class);
	}

}

Index: AllTests.java
===================================================================
RCS file: /cvsroot/junit/junit/src/org/junit/tests/AllTests.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- AllTests.java	27 Dec 2006 17:45:54 -0000	1.4
+++ AllTests.java	27 Dec 2006 22:27:22 -0000	1.5
@@ -8,6 +8,7 @@
 
 @RunWith(Suite.class)
 @SuiteClasses({
+	CustomMethodRunnerTest.class,
 	ListenerTest.class,
 	FailedConstructionTest.class,
 	MixInTest.class,


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.