New 4.7 snapshot release: get the name of the current test

David Saff <[email protected]> Mon, 11 May 2009 23:54:39 -0400
Newsgroups gmane.comp.java.junit.devel,gmane.comp.java.junit.user
Message-ID <[email protected]>
All,

A new snapshot release of JUnit 4.7 is up:

https://sourceforge.net/project/showfiles.php?group_id=15278&package_id=226053&release_id=682045

The newest feature in this release is a golden oldie: access to the
name of the currently-running test method:

	@RunWith(Interceptors.class)
	public class NameInterceptorTest {
		@Interceptor public TestName name = new TestName();
		
		@Test public void testA() {
			assertEquals("testA", name.getMethodName());
		}
	}

Share and Enjoy,

   David Saff


P.S. The entire release notes:

## Summary of Changes in version 4.7 ##

### Interceptors ###

- Interceptors allow very flexible addition or redefinition of the behavior
  of each test method in a test class.  For example, this class will
  keep a log of each passing and failing test:

	@RunWith(Interceptors.class)
	public static class WatchmanTest {
		private static String watchedLog;

		@Interceptor
		public StatementInterceptor watchman= new TestWatchman() {
			@Override
			public void failed(Throwable e, FrameworkMethod method) {
				watchedLog+= method.getName() + " "
						+ e.getClass().getSimpleName() + "\n";
			}

			@Override
			public void succeeded(FrameworkMethod method) {
				watchedLog+= method.getName() + " " + "success!\n";
			}
		};

		@Test
		public void fails() {
			fail();
		}

		@Test
		public void succeeds() {
		}
	}

	For more on this feature, see http://www.threeriversinstitute.org/blog/?p=155

- The TestName Interceptor makes the current test name available
inside test methods:

	@RunWith(Interceptors.class)
	public class NameInterceptorTest {
		@Interceptor public TestName name = new TestName();
		
		@Test public void testA() {
			assertEquals("testA", name.getMethodName());
		}
		
		@Test public void testB() {
			assertEquals("testB", name.getMethodName());
		}
	}

- The Timeout Interceptor applies the same timeout to all test methods
in a class:

	@RunWith(Interceptors.class)
	public static class HasGlobalTimeout {
		public static String log;
		
		@Interceptor public StatementInterceptor globalTimeout = new Timeout(20);
		
		@Test public void testInfiniteLoop1() {
			log+= "ran1";
			for(;;) {}
		}
		
		@Test public void testInfiniteLoop2() {
			log+= "ran2";
			for(;;) {}
		}
	}

### Timeouts ###
- Tests that timeout now show the stack trace of the test thread.

### Docs ###
- Javadocs now link to online JDK javadocs (bug 2090230)
- Parameterized runner javadocs improved (bug 2186792)
- Fixed Javadoc code sample for AfterClass (2126279)
- Fixed Javadoc for assertArraysEqual(float[], float[])

### Bug fixes ###
- Fixed: BaseTestRunner.getTest() requires class to extend TestCase (1812200)
- Fixed: Suite does not allow for inheritance in annotations (2783118)
- Fixed: ParallelComputer skipped tests that took longer than 2 seconds

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image 
processing features enabled. http://p.sf.net/sfu/kodak-com