⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmethodtest.java

📁 JUNIT1.4源文件等等 源文件等等 源文件等等 源文件
💻 JAVA
字号:
package org.junit.tests;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertTrue;import java.util.Collections;import java.util.List;import junit.framework.JUnit4TestAdapter;import junit.framework.TestResult;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Ignore;import org.junit.Test;import org.junit.internal.runners.InitializationError;import org.junit.internal.runners.MethodValidator;import org.junit.internal.runners.TestClass;import org.junit.internal.runners.JUnit4ClassRunner;import org.junit.runner.JUnitCore;import org.junit.runner.Result;public class TestMethodTest {	@SuppressWarnings("all")  	public static class EverythingWrong {		private EverythingWrong() {}		@BeforeClass public void notStaticBC() {}		@BeforeClass static void notPublicBC() {}		@BeforeClass public static int nonVoidBC() { return 0; }		@BeforeClass public static void argumentsBC(int i) {}		@BeforeClass public static void fineBC() {}		@AfterClass public void notStaticAC() {}		@AfterClass static void notPublicAC() {}		@AfterClass public static int nonVoidAC() { return 0; }		@AfterClass public static void argumentsAC(int i) {}		@AfterClass public static void fineAC() {}		@After public static void staticA() {}		@After void notPublicA() {}		@After public int nonVoidA() { return 0; }		@After public void argumentsA(int i) {}		@After public void fineA() {}		@Before public static void staticB() {}		@Before void notPublicB() {}		@Before public int nonVoidB() { return 0; }		@Before public void argumentsB(int i) {}		@Before public void fineB() {}		@Test public static void staticT() {}		@Test void notPublicT() {}		@Test public int nonVoidT() { return 0; }		@Test public void argumentsT(int i) {}		@Test public void fineT() {}	}		@Test public void testFailures() throws Exception {		List<Throwable> problems= validateAllMethods(EverythingWrong.class);		int errorCount= 1 + 4 * 5; // missing constructor plus four invalid methods for each annotation */		assertEquals(errorCount, problems.size());	}	static public class SuperWrong {		@Test void notPublic() {		}	}	static public class SubWrong extends SuperWrong {		@Test public void justFine() {		}	}	@Test public void validateInheritedMethods() throws Exception {		List<Throwable> problems= validateAllMethods(SubWrong.class);		assertEquals(1, problems.size());	}	static public class SubShadows extends SuperWrong {		@Override		@Test public void notPublic() {		}	}	@Test public void dontValidateShadowedMethods() throws Exception {		List<Throwable> problems= validateAllMethods(SubShadows.class);		assertTrue(problems.isEmpty());	}	private List<Throwable> validateAllMethods(Class<?> clazz) {		try {			new JUnit4ClassRunner(clazz);		} catch (InitializationError e) {			return e.getCauses();		}		return Collections.emptyList();	}	static public class IgnoredTest {		@Test public void valid() {}		@Ignore @Test public void ignored() {}		@Ignore("For testing purposes") @Test public void withReason() {}	}	@Test public void ignoreRunner() {		JUnitCore runner= new JUnitCore();		Result result= runner.run(IgnoredTest.class);		assertEquals(2, result.getIgnoreCount());	}	@Test public void compatibility() {		TestResult result= new TestResult();		new JUnit4TestAdapter(IgnoredTest.class).run(result);		assertEquals(1, result.runCount());	}		public static class Confused {		@Test public void a(Object b) {		}				@Test public void a() {		}	}		@Test public void overloaded() {		MethodValidator validator= new MethodValidator(new TestClass(Confused.class));		List<Throwable> errors= validator.validateMethodsForDefaultRunner();		assertEquals(1, errors.size());	}		public static class OnlyTestIsIgnored {		@Ignore @Test public void ignored() {}	}		@Test public void onlyIgnoredMethodsIsStillFineTestClass() {		Result result= JUnitCore.runClasses(OnlyTestIsIgnored.class);		assertEquals(0, result.getFailureCount());		assertEquals(1, result.getIgnoreCount());	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -