summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/validation/BadlyFormedClassesTest.java
blob: ff61822b936eca94bc2faa8641cb83c535b21cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
	}
}