1 package org.junit.tests.experimental.theories.runner;
2
3 import static org.hamcrest.CoreMatchers.containsString;
4 import static org.hamcrest.CoreMatchers.is;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertThat;
7 import static org.junit.experimental.results.PrintableResult.testResult;
8 import static org.junit.experimental.results.ResultMatchers.failureCountIs;
9 import static org.junit.experimental.results.ResultMatchers.isSuccessful;
10
11 import org.junit.Test;
12 import org.junit.experimental.theories.DataPoint;
13 import org.junit.experimental.theories.Theories;
14 import org.junit.runner.JUnitCore;
15 import org.junit.runner.Result;
16 import org.junit.runner.RunWith;
17
18 public class WithOnlyTestAnnotations {
19 @RunWith(Theories.class)
20 public static class HonorExpectedException {
21 @Test(expected = NullPointerException.class)
22 public void shouldThrow() {
23
24 }
25 }
26
27 @Test
28 public void honorExpected() throws Exception {
29 assertThat(testResult(HonorExpectedException.class).failureCount(), is(1));
30 }
31
32 @RunWith(Theories.class)
33 public static class HonorExpectedExceptionPasses {
34 @Test(expected = NullPointerException.class)
35 public void shouldThrow() {
36 throw new NullPointerException();
37 }
38 }
39
40 @Test
41 public void honorExpectedPassing() throws Exception {
42 assertThat(testResult(HonorExpectedExceptionPasses.class), isSuccessful());
43 }
44
45 @RunWith(Theories.class)
46 public static class HonorTimeout {
47 @Test(timeout = 5)
48 public void shouldStop() {
49 while (true) {
50 try {
51 Thread.sleep(1000);
52 } catch (InterruptedException e) {
53
54 }
55 }
56 }
57 }
58
59 @Test
60 public void honorTimeout() throws Exception {
61 assertThat(testResult(HonorTimeout.class), failureCountIs(1));
62 }
63
64 @RunWith(Theories.class)
65 static public class ErrorWhenTestHasParametersDespiteTheories {
66 @DataPoint
67 public static int ZERO = 0;
68
69 @Test
70 public void testMethod(int i) {
71 }
72 }
73
74 @Test
75 public void testErrorWhenTestHasParametersDespiteTheories() {
76 JUnitCore core = new JUnitCore();
77 Result result = core.run(ErrorWhenTestHasParametersDespiteTheories.class);
78 assertEquals(1, result.getFailureCount());
79 String message = result.getFailures().get(0).getMessage();
80 assertThat(message, containsString("should have no parameters"));
81 }
82 }