diff --git a/src/main/java/org/junit/internal/runners/ClassRoadie.java b/src/main/java/org/junit/internal/runners/ClassRoadie.java deleted file mode 100644 index f2cf7d8443f0..000000000000 --- a/src/main/java/org/junit/internal/runners/ClassRoadie.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; - -import org.junit.internal.AssumptionViolatedException; -import org.junit.runner.Description; -import org.junit.runner.notification.Failure; -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -public class ClassRoadie { - private RunNotifier fNotifier; - private TestClass fTestClass; - private Description fDescription; - private final Runnable fRunnable; - - public ClassRoadie(RunNotifier notifier, TestClass testClass, - Description description, Runnable runnable) { - fNotifier = notifier; - fTestClass = testClass; - fDescription = description; - fRunnable = runnable; - } - - protected void runUnprotected() { - fRunnable.run(); - } - - ; - - protected void addFailure(Throwable targetException) { - fNotifier.fireTestFailure(new Failure(fDescription, targetException)); - } - - public void runProtected() { - try { - runBefores(); - runUnprotected(); - } catch (FailedBefore e) { - } finally { - runAfters(); - } - } - - private void runBefores() throws FailedBefore { - try { - try { - List befores = fTestClass.getBefores(); - for (Method before : befores) { - before.invoke(null); - } - } catch (InvocationTargetException e) { - throw e.getTargetException(); - } - } catch (AssumptionViolatedException e) { - throw new FailedBefore(); - } catch (Throwable e) { - addFailure(e); - throw new FailedBefore(); - } - } - - private void runAfters() { - List afters = fTestClass.getAfters(); - for (Method after : afters) { - try { - after.invoke(null); - } catch (InvocationTargetException e) { - addFailure(e.getTargetException()); - } catch (Throwable e) { - addFailure(e); // Untested, but seems impossible - } - } - } -} diff --git a/src/main/java/org/junit/internal/runners/FailedBefore.java b/src/main/java/org/junit/internal/runners/FailedBefore.java deleted file mode 100644 index ed1fa6b1d4cf..000000000000 --- a/src/main/java/org/junit/internal/runners/FailedBefore.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.junit.internal.runners; - -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -class FailedBefore extends Exception { - private static final long serialVersionUID = 1L; -} \ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java b/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java deleted file mode 100644 index 8e3480bbe3e5..000000000000 --- a/src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java +++ /dev/null @@ -1,149 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; - -import org.junit.runner.Description; -import org.junit.runner.Runner; -import org.junit.runner.manipulation.Filter; -import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.NoTestsRemainException; -import org.junit.runner.manipulation.Sortable; -import org.junit.runner.manipulation.Sorter; -import org.junit.runner.notification.Failure; -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - * - * This may disappear as soon as 1 April 2009 - */ -@Deprecated -public class JUnit4ClassRunner extends Runner implements Filterable, Sortable { - private final List fTestMethods; - private TestClass fTestClass; - - public JUnit4ClassRunner(Class klass) throws InitializationError { - fTestClass = new TestClass(klass); - fTestMethods = getTestMethods(); - validate(); - } - - protected List getTestMethods() { - return fTestClass.getTestMethods(); - } - - protected void validate() throws InitializationError { - MethodValidator methodValidator = new MethodValidator(fTestClass); - methodValidator.validateMethodsForDefaultRunner(); - methodValidator.assertValid(); - } - - @Override - public void run(final RunNotifier notifier) { - new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() { - public void run() { - runMethods(notifier); - } - }).runProtected(); - } - - protected void runMethods(final RunNotifier notifier) { - for (Method method : fTestMethods) { - invokeTestMethod(method, notifier); - } - } - - @Override - public Description getDescription() { - Description spec = Description.createSuiteDescription(getName(), classAnnotations()); - List testMethods = fTestMethods; - for (Method method : testMethods) { - spec.addChild(methodDescription(method)); - } - return spec; - } - - protected Annotation[] classAnnotations() { - return fTestClass.getJavaClass().getAnnotations(); - } - - protected String getName() { - return getTestClass().getName(); - } - - protected Object createTest() throws Exception { - return getTestClass().getConstructor().newInstance(); - } - - protected void invokeTestMethod(Method method, RunNotifier notifier) { - Description description = methodDescription(method); - Object test; - try { - test = createTest(); - } catch (InvocationTargetException e) { - testAborted(notifier, description, e.getCause()); - return; - } catch (Exception e) { - testAborted(notifier, description, e); - return; - } - TestMethod testMethod = wrapMethod(method); - new MethodRoadie(test, testMethod, notifier, description).run(); - } - - private void testAborted(RunNotifier notifier, Description description, - Throwable e) { - notifier.fireTestStarted(description); - notifier.fireTestFailure(new Failure(description, e)); - notifier.fireTestFinished(description); - } - - protected TestMethod wrapMethod(Method method) { - return new TestMethod(method, fTestClass); - } - - protected String testName(Method method) { - return method.getName(); - } - - protected Description methodDescription(Method method) { - return Description.createTestDescription(getTestClass().getJavaClass(), testName(method), testAnnotations(method)); - } - - protected Annotation[] testAnnotations(Method method) { - return method.getAnnotations(); - } - - public void filter(Filter filter) throws NoTestsRemainException { - for (Iterator iter = fTestMethods.iterator(); iter.hasNext(); ) { - Method method = iter.next(); - if (!filter.shouldRun(methodDescription(method))) { - iter.remove(); - } - } - if (fTestMethods.isEmpty()) { - throw new NoTestsRemainException(); - } - } - - public void sort(final Sorter sorter) { - Collections.sort(fTestMethods, new Comparator() { - public int compare(Method o1, Method o2) { - return sorter.compare(methodDescription(o1), methodDescription(o2)); - } - }); - } - - protected TestClass getTestClass() { - return fTestClass; - } -} \ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/MethodRoadie.java b/src/main/java/org/junit/internal/runners/MethodRoadie.java deleted file mode 100644 index d417551afd9c..000000000000 --- a/src/main/java/org/junit/internal/runners/MethodRoadie.java +++ /dev/null @@ -1,163 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.internal.AssumptionViolatedException; -import org.junit.runner.Description; -import org.junit.runner.notification.Failure; -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.junit.runners.model.TestTimedOutException; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -public class MethodRoadie { - private final Object fTest; - private final RunNotifier fNotifier; - private final Description fDescription; - private TestMethod fTestMethod; - - public MethodRoadie(Object test, TestMethod method, RunNotifier notifier, Description description) { - fTest = test; - fNotifier = notifier; - fDescription = description; - fTestMethod = method; - } - - public void run() { - if (fTestMethod.isIgnored()) { - fNotifier.fireTestIgnored(fDescription); - return; - } - fNotifier.fireTestStarted(fDescription); - try { - long timeout = fTestMethod.getTimeout(); - if (timeout > 0) { - runWithTimeout(timeout); - } else { - runTest(); - } - } finally { - fNotifier.fireTestFinished(fDescription); - } - } - - private void runWithTimeout(final long timeout) { - runBeforesThenTestThenAfters(new Runnable() { - - public void run() { - ExecutorService service = Executors.newSingleThreadExecutor(); - Callable callable = new Callable() { - public Object call() throws Exception { - runTestMethod(); - return null; - } - }; - Future result = service.submit(callable); - service.shutdown(); - try { - boolean terminated = service.awaitTermination(timeout, - TimeUnit.MILLISECONDS); - if (!terminated) { - service.shutdownNow(); - } - result.get(0, TimeUnit.MILLISECONDS); // throws the exception if one occurred during the invocation - } catch (TimeoutException e) { - addFailure(new TestTimedOutException(timeout, TimeUnit.MILLISECONDS)); - } catch (Exception e) { - addFailure(e); - } - } - }); - } - - public void runTest() { - runBeforesThenTestThenAfters(new Runnable() { - public void run() { - runTestMethod(); - } - }); - } - - public void runBeforesThenTestThenAfters(Runnable test) { - try { - runBefores(); - test.run(); - } catch (FailedBefore e) { - } catch (Exception e) { - throw new RuntimeException("test should never throw an exception to this level"); - } finally { - runAfters(); - } - } - - protected void runTestMethod() { - try { - fTestMethod.invoke(fTest); - if (fTestMethod.expectsException()) { - addFailure(new AssertionError("Expected exception: " + fTestMethod.getExpectedException().getName())); - } - } catch (InvocationTargetException e) { - Throwable actual = e.getTargetException(); - if (actual instanceof AssumptionViolatedException) { - return; - } else if (!fTestMethod.expectsException()) { - addFailure(actual); - } else if (fTestMethod.isUnexpected(actual)) { - String message = "Unexpected exception, expected<" + fTestMethod.getExpectedException().getName() + "> but was<" - + actual.getClass().getName() + ">"; - addFailure(new Exception(message, actual)); - } - } catch (Throwable e) { - addFailure(e); - } - } - - private void runBefores() throws FailedBefore { - try { - try { - List befores = fTestMethod.getBefores(); - for (Method before : befores) { - before.invoke(fTest); - } - } catch (InvocationTargetException e) { - throw e.getTargetException(); - } - } catch (AssumptionViolatedException e) { - throw new FailedBefore(); - } catch (Throwable e) { - addFailure(e); - throw new FailedBefore(); - } - } - - private void runAfters() { - List afters = fTestMethod.getAfters(); - for (Method after : afters) { - try { - after.invoke(fTest); - } catch (InvocationTargetException e) { - addFailure(e.getTargetException()); - } catch (Throwable e) { - addFailure(e); // Untested, but seems impossible - } - } - } - - protected void addFailure(Throwable e) { - fNotifier.fireTestFailure(new Failure(fDescription, e)); - } -} - diff --git a/src/main/java/org/junit/internal/runners/MethodValidator.java b/src/main/java/org/junit/internal/runners/MethodValidator.java deleted file mode 100644 index 3bec0cfd7071..000000000000 --- a/src/main/java/org/junit/internal/runners/MethodValidator.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -public class MethodValidator { - - private final List fErrors = new ArrayList(); - - private TestClass fTestClass; - - public MethodValidator(TestClass testClass) { - fTestClass = testClass; - } - - public void validateInstanceMethods() { - validateTestMethods(After.class, false); - validateTestMethods(Before.class, false); - validateTestMethods(Test.class, false); - - List methods = fTestClass.getAnnotatedMethods(Test.class); - if (methods.size() == 0) { - fErrors.add(new Exception("No runnable methods")); - } - } - - public void validateStaticMethods() { - validateTestMethods(BeforeClass.class, true); - validateTestMethods(AfterClass.class, true); - } - - public List validateMethodsForDefaultRunner() { - validateNoArgConstructor(); - validateStaticMethods(); - validateInstanceMethods(); - return fErrors; - } - - public void assertValid() throws InitializationError { - if (!fErrors.isEmpty()) { - throw new InitializationError(fErrors); - } - } - - public void validateNoArgConstructor() { - try { - fTestClass.getConstructor(); - } catch (Exception e) { - fErrors.add(new Exception("Test class should have public zero-argument constructor", e)); - } - } - - private void validateTestMethods(Class annotation, - boolean isStatic) { - List methods = fTestClass.getAnnotatedMethods(annotation); - - for (Method each : methods) { - if (Modifier.isStatic(each.getModifiers()) != isStatic) { - String state = isStatic ? "should" : "should not"; - fErrors.add(new Exception("Method " + each.getName() + "() " - + state + " be static")); - } - if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) { - fErrors.add(new Exception("Class " + each.getDeclaringClass().getName() - + " should be public")); - } - if (!Modifier.isPublic(each.getModifiers())) { - fErrors.add(new Exception("Method " + each.getName() - + " should be public")); - } - if (each.getReturnType() != Void.TYPE) { - fErrors.add(new Exception("Method " + each.getName() - + " should be void")); - } - if (each.getParameterTypes().length != 0) { - fErrors.add(new Exception("Method " + each.getName() - + " should have no parameters")); - } - } - } -} diff --git a/src/main/java/org/junit/internal/runners/TestClass.java b/src/main/java/org/junit/internal/runners/TestClass.java deleted file mode 100644 index 9d2e10803680..000000000000 --- a/src/main/java/org/junit/internal/runners/TestClass.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.internal.MethodSorter; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -public class TestClass { - private final Class fClass; - - public TestClass(Class klass) { - fClass = klass; - } - - public List getTestMethods() { - return getAnnotatedMethods(Test.class); - } - - List getBefores() { - return getAnnotatedMethods(BeforeClass.class); - } - - List getAfters() { - return getAnnotatedMethods(AfterClass.class); - } - - public List getAnnotatedMethods(Class annotationClass) { - List results = new ArrayList(); - for (Class eachClass : getSuperClasses(fClass)) { - Method[] methods = MethodSorter.getDeclaredMethods(eachClass); - for (Method eachMethod : methods) { - Annotation annotation = eachMethod.getAnnotation(annotationClass); - if (annotation != null && !isShadowed(eachMethod, results)) { - results.add(eachMethod); - } - } - } - if (runsTopToBottom(annotationClass)) { - Collections.reverse(results); - } - return results; - } - - private boolean runsTopToBottom(Class annotation) { - return annotation.equals(Before.class) || annotation.equals(BeforeClass.class); - } - - private boolean isShadowed(Method method, List results) { - for (Method each : results) { - if (isShadowed(method, each)) { - return true; - } - } - return false; - } - - private boolean isShadowed(Method current, Method previous) { - if (!previous.getName().equals(current.getName())) { - return false; - } - if (previous.getParameterTypes().length != current.getParameterTypes().length) { - return false; - } - for (int i = 0; i < previous.getParameterTypes().length; i++) { - if (!previous.getParameterTypes()[i].equals(current.getParameterTypes()[i])) { - return false; - } - } - return true; - } - - private List> getSuperClasses(Class testClass) { - ArrayList> results = new ArrayList>(); - Class current = testClass; - while (current != null) { - results.add(current); - current = current.getSuperclass(); - } - return results; - } - - public Constructor getConstructor() throws SecurityException, NoSuchMethodException { - return fClass.getConstructor(); - } - - public Class getJavaClass() { - return fClass; - } - - public String getName() { - return fClass.getName(); - } - -} diff --git a/src/main/java/org/junit/internal/runners/TestMethod.java b/src/main/java/org/junit/internal/runners/TestMethod.java deleted file mode 100644 index c957bc80359c..000000000000 --- a/src/main/java/org/junit/internal/runners/TestMethod.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.junit.internal.runners; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.Test.None; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * @deprecated Included for backwards compatibility with JUnit 4.4. Will be - * removed in the next release. Please use - * {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}. - */ -@Deprecated -public class TestMethod { - private final Method fMethod; - private TestClass fTestClass; - - public TestMethod(Method method, TestClass testClass) { - fMethod = method; - fTestClass = testClass; - } - - public boolean isIgnored() { - return fMethod.getAnnotation(Ignore.class) != null; - } - - public long getTimeout() { - Test annotation = fMethod.getAnnotation(Test.class); - if (annotation == null) { - return 0; - } - long timeout = annotation.timeout(); - return timeout; - } - - protected Class getExpectedException() { - Test annotation = fMethod.getAnnotation(Test.class); - if (annotation == null || annotation.expected() == None.class) { - return null; - } else { - return annotation.expected(); - } - } - - boolean isUnexpected(Throwable exception) { - return !getExpectedException().isAssignableFrom(exception.getClass()); - } - - boolean expectsException() { - return getExpectedException() != null; - } - - List getBefores() { - return fTestClass.getAnnotatedMethods(Before.class); - } - - List getAfters() { - return fTestClass.getAnnotatedMethods(After.class); - } - - public void invoke(Object test) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { - fMethod.invoke(test); - } - -} diff --git a/src/test/java/org/junit/tests/AllTests.java b/src/test/java/org/junit/tests/AllTests.java index 51360367220c..0b827d6a43f9 100644 --- a/src/test/java/org/junit/tests/AllTests.java +++ b/src/test/java/org/junit/tests/AllTests.java @@ -21,7 +21,6 @@ import org.junit.tests.assertion.AssertionTest; import org.junit.tests.assertion.ComparisonFailureTest; import org.junit.tests.assertion.MultipleFailureExceptionTest; -import org.junit.tests.deprecated.JUnit4ClassRunnerTest; import org.junit.tests.description.AnnotatedDescriptionTest; import org.junit.tests.description.SuiteDescriptionTest; import org.junit.tests.description.TestDescriptionTest; @@ -96,7 +95,6 @@ import org.junit.tests.running.methods.ParameterizedTestMethodTest; import org.junit.tests.running.methods.TestMethodTest; import org.junit.tests.running.methods.TimeoutTest; -import org.junit.tests.validation.BadlyFormedClassesTest; import org.junit.tests.validation.FailedConstructionTest; import org.junit.tests.validation.InaccessibleBaseClassTest; import org.junit.tests.validation.ValidationTest; @@ -104,7 +102,6 @@ // These test files need to be cleaned. See // https://sourceforge.net/pm/task.php?func=detailtask&project_task_id=136507&group_id=15278&group_project_id=51407 -@SuppressWarnings("deprecation") @RunWith(Suite.class) @SuiteClasses({ AssumptionTest.class, @@ -143,7 +140,6 @@ JUnitCoreReturnsCorrectExitCodeTest.class, InaccessibleBaseClassTest.class, SuiteMethodTest.class, - BadlyFormedClassesTest.class, IgnoreClassTest.class, OldTestClassAdaptingListenerTest.class, AnnotatedDescriptionTest.class, @@ -160,7 +156,6 @@ MatcherTest.class, ObjectContractTest.class, TheoriesPerformanceTest.class, - JUnit4ClassRunnerTest.class, UseSuiteAsASuperclassTest.class, FilterableTest.class, FilterTest.class, diff --git a/src/test/java/org/junit/tests/deprecated/JUnit4ClassRunnerTest.java b/src/test/java/org/junit/tests/deprecated/JUnit4ClassRunnerTest.java deleted file mode 100644 index 43983ba97637..000000000000 --- a/src/test/java/org/junit/tests/deprecated/JUnit4ClassRunnerTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.junit.tests.deprecated; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.junit.internal.runners.JUnit4ClassRunner; -import org.junit.runner.JUnitCore; -import org.junit.runner.Result; -import org.junit.runner.RunWith; - -/** - * @deprecated This is a simple smoke test to make sure the old JUnit4ClassRunner basically works. - * Delete this test when JUnit4ClassRunner goes to the Great Heap In The Sky. - */ -@Deprecated -public class JUnit4ClassRunnerTest { - - @SuppressWarnings("deprecation") - @RunWith(JUnit4ClassRunner.class) - public static class Example { - @Test - public void success() { - } - - @Test - public void failure() { - fail(); - } - } - - @Test - public void runWithOldJUnit4ClassRunner() { - Result result = JUnitCore.runClasses(Example.class); - assertThat(result.getRunCount(), is(2)); - assertThat(result.getFailureCount(), is(1)); - } - - @SuppressWarnings("deprecation") - @RunWith(JUnit4ClassRunner.class) - public static class UnconstructableExample { - public UnconstructableExample() { - throw new UnsupportedOperationException(); - } - - @Test - public void success() { - } - - @Test - public void failure() { - fail(); - } - } - - - @Test - public void runWithOldJUnit4ClassRunnerAndBadConstructor() { - Result result = JUnitCore.runClasses(UnconstructableExample.class); - assertThat(result.getRunCount(), is(2)); - assertThat(result.getFailureCount(), is(2)); - } -} diff --git a/src/test/java/org/junit/tests/validation/BadlyFormedClassesTest.java b/src/test/java/org/junit/tests/validation/BadlyFormedClassesTest.java deleted file mode 100644 index 44cf26479f4c..000000000000 --- a/src/test/java/org/junit/tests/validation/BadlyFormedClassesTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.junit.tests.validation; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; -import org.junit.internal.runners.JUnit4ClassRunner; -import org.junit.runner.JUnitCore; -import org.junit.runner.Result; -import org.junit.runner.RunWith; -import org.junit.runner.notification.Failure; - -@SuppressWarnings("deprecation") -public class BadlyFormedClassesTest { - public static class FaultyConstructor { - public FaultyConstructor() throws Exception { - throw new Exception("Thrown during construction"); - } - - @Test - public void someTest() { - /* - * Empty test just to fool JUnit and IDEs into running this class as - * a JUnit test - */ - } - } - - ; - - @RunWith(JUnit4ClassRunner.class) - public static class BadBeforeMethodWithLegacyRunner { - @Before - void before() { - - } - - @Test - public void someTest() { - } - } - - ; - - public static class NoTests { - // class without tests - } - - @Test - public void constructorException() { - String message = exceptionMessageFrom(FaultyConstructor.class); - assertEquals("Thrown during construction", message); - } - - @Test - public void noRunnableMethods() { - assertEquals("No runnable methods", exceptionMessageFrom(NoTests.class)); - } - - @Test - public void badBeforeMethodWithLegacyRunner() { - assertEquals("Method before should be public", - exceptionMessageFrom(BadBeforeMethodWithLegacyRunner.class)); - } - - private String exceptionMessageFrom(Class testClass) { - JUnitCore core = new JUnitCore(); - Result result = core.run(testClass); - Failure failure = result.getFailures().get(0); - String message = failure.getException().getMessage(); - return message; - } -}