|
23 | 23 | import android.os.Bundle;
|
24 | 24 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
25 | 25 | import androidx.test.filters.SmallTest;
|
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
26 | 28 | import junit.framework.Assert;
|
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.BeforeClass; |
27 | 31 | import org.junit.Test;
|
| 32 | +import org.junit.runner.Computer; |
28 | 33 | import org.junit.runner.Description;
|
| 34 | +import org.junit.runner.JUnitCore; |
| 35 | +import org.junit.runner.Request; |
29 | 36 | import org.junit.runner.RunWith;
|
30 |
| -import org.junit.runner.notification.Failure; |
31 | 37 |
|
32 | 38 | @RunWith(AndroidJUnit4.class)
|
33 | 39 | @SmallTest
|
@@ -62,27 +68,50 @@ public void sendStatus(int code, Bundle bundle) {
|
62 | 68 | assertTrue(resultBundle[0].containsKey(REPORT_KEY_STACK));
|
63 | 69 | }
|
64 | 70 |
|
65 |
| - @Test |
66 |
| - public void verifyFailureDescriptionPropagatedToStartAndFinishMethods() throws Exception { |
67 |
| - Description[] descriptions = new Description[2]; |
68 |
| - InstrumentationResultPrinter intrResultPrinter = |
69 |
| - new InstrumentationResultPrinter() { |
70 |
| - @Override |
71 |
| - public void testStarted(Description description) throws Exception { |
72 |
| - descriptions[0] = description; |
73 |
| - } |
| 71 | + private static class TestInstrumentationResultPrinter extends InstrumentationResultPrinter { |
| 72 | + final List<String> resultsLog = new ArrayList<>(); |
74 | 73 |
|
75 |
| - @Override |
76 |
| - public void testFinished(Description description) throws Exception { |
77 |
| - descriptions[1] = description; |
78 |
| - } |
79 |
| - }; |
| 74 | + @Override |
| 75 | + public void sendStatus(int code, Bundle bundle) { |
| 76 | + resultsLog.add( |
| 77 | + String.format( |
| 78 | + "code=%s, name=%s#%s", |
| 79 | + code, |
| 80 | + bundle.getString(REPORT_KEY_NAME_CLASS), |
| 81 | + bundle.getString(REPORT_KEY_NAME_TEST))); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static class ThrowingTest { |
| 86 | + @BeforeClass |
| 87 | + public static void setUpClass() { |
| 88 | + throw new RuntimeException(); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterClass |
| 92 | + public static void tearDownClass() { |
| 93 | + throw new RuntimeException(); |
| 94 | + } |
80 | 95 |
|
81 |
| - Description d = Description.createTestDescription(this.getClass(), "Failure Description"); |
82 |
| - Failure testFailure = new Failure(d, new Exception()); |
83 |
| - intrResultPrinter.testFailure(testFailure); |
| 96 | + @Test |
| 97 | + public void emptyTest() {} |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void verifyBeforeClassExceptionsReported() throws Exception { |
| 102 | + JUnitCore core = new JUnitCore(); |
| 103 | + var intrResultPrinter = new TestInstrumentationResultPrinter(); |
| 104 | + core.addListener(intrResultPrinter); |
| 105 | + Request testRequest = Request.classes(new Computer(), new Class<?>[] {ThrowingTest.class}); |
| 106 | + core.run(testRequest); |
84 | 107 |
|
85 |
| - assertEquals(d, descriptions[0]); |
86 |
| - assertEquals(d, descriptions[1]); |
| 108 | + String className = ThrowingTest.class.getName(); |
| 109 | + assertEquals( |
| 110 | + List.of( |
| 111 | + "code=1, name=" + className + "#null", |
| 112 | + "code=-2, name=" + className + "#null", |
| 113 | + "code=1, name=" + className + "#null", |
| 114 | + "code=-2, name=" + className + "#null"), |
| 115 | + intrResultPrinter.resultsLog); |
87 | 116 | }
|
88 | 117 | }
|
0 commit comments