| 1 |
/* |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright (c) 2024 Todd Ginsberg |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
package com.ginsberg.junit.exit; |
| 26 |
|
| 27 |
import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 28 |
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 29 |
import org.junit.platform.launcher.core.LauncherFactory; |
| 30 |
import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 31 |
import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 32 |
|
| 33 |
import java.util.HashSet; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 |
import static org.junit.jupiter.api.Assertions.fail; |
| 38 |
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
| 39 |
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod; |
| 40 |
|
| 41 |
class TestUtils { |
| 42 |
|
| 43 |
static void assertTestFails(final Class<?> clazz, final String testMethod) { |
| 44 |
final SummaryGeneratingListener listener = executeTest(clazz, testMethod); |
| 45 |
|
| 46 |
assertEquals(1, listener.getSummary().getTestsFoundCount(), "Should have found one test"); |
| 47 |
assertEquals(1, listener.getSummary().getTestsFailedCount(), "Single test should have failed"); |
| 48 |
} |
| 49 |
|
| 50 |
static void assertTestFails(final Class<?> clazz) { |
| 51 |
final SummaryGeneratingListener listener = executeTest(clazz, null); |
| 52 |
|
| 53 |
assertEquals(1, listener.getSummary().getTestsFoundCount(), "Should have found one test"); |
| 54 |
assertEquals(1, listener.getSummary().getTestsFailedCount(), "Single test should have failed"); |
| 55 |
} |
| 56 |
|
| 57 |
static void assertTestFailsExceptionally( |
| 58 |
final Class<?> clazz, |
| 59 |
final String testMethod, |
| 60 |
final Class<? extends Exception> expectedExceptionClass |
| 61 |
) { |
| 62 |
final SummaryGeneratingListener listener = executeTest(clazz, testMethod); |
| 63 |
assertEquals(1, listener.getSummary().getFailures().size(), "Test did not finish exceptionally"); |
| 64 |
assertEquals( |
| 65 |
expectedExceptionClass, |
| 66 |
listener.getSummary().getFailures().get(0).getException().getClass(), |
| 67 |
"Failed exceptionally but not because of the expected exception" |
| 68 |
); |
| 69 |
|
| 70 |
assertEquals(1, listener.getSummary().getTestsFoundCount(), "Should have found one test"); |
| 71 |
assertEquals(1, listener.getSummary().getTestsFailedCount(), "Single test should have failed"); |
| 72 |
} |
| 73 |
|
| 74 |
static void assertParameterizedTestFails(final Class<?> clazz, final Boolean... expectedSuccess) { |
| 75 |
final SummaryGeneratingListener listener = executeTest(clazz, null); |
| 76 |
final Set<Integer> failedTestNumbers = new HashSet<>(); |
| 77 |
for(final TestExecutionSummary.Failure failure : listener.getSummary().getFailures()) { |
| 78 |
failedTestNumbers.add( |
| 79 |
Integer.parseInt(failure.getTestIdentifier().getDisplayName()) -1 |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
assertEquals(expectedSuccess.length, listener.getSummary().getTestsFoundCount(), "Wrong number of tests found"); |
| 84 |
for(int testNumber = 0; testNumber < expectedSuccess.length; testNumber++) { |
| 85 |
if(expectedSuccess[testNumber] && failedTestNumbers.contains(testNumber)) { |
| 86 |
fail(String.format("Test %d should have succeeded, but didn't", testNumber)); |
| 87 |
} else if(!expectedSuccess[testNumber] && !failedTestNumbers.contains(testNumber)) { |
| 88 |
fail(String.format("Test %d should have failed, but didn't", testNumber)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
static void assertTestSucceeds(final Class<?> clazz, final String testMethod) { |
| 95 |
final SummaryGeneratingListener listener = executeTest(clazz, testMethod); |
| 96 |
|
| 97 |
assertEquals(1, listener.getSummary().getTestsFoundCount(), "Should have found one test"); |
| 98 |
assertEquals(1, listener.getSummary().getTestsSucceededCount(), "Single test should have succeeded"); |
| 99 |
} |
| 100 |
|
| 101 |
static void assertTestSucceeds(final Class<?> clazz) { |
| 102 |
final SummaryGeneratingListener listener = executeTest(clazz, null); |
| 103 |
|
| 104 |
assertEquals(1, listener.getSummary().getTestsFoundCount(), "Should have found one test"); |
| 105 |
assertEquals(1, listener.getSummary().getTestsSucceededCount(), "Single test should have succeeded"); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Execute the given test and then return a summary of its execution. This is used for tests that |
| 110 |
* succeed when other tests fail ("Test that a test decorated with X fails when...") |
| 111 |
*/ |
| 112 |
private static SummaryGeneratingListener executeTest(final Class<?> clazz, final String testMethod) { |
| 113 |
final SummaryGeneratingListener listener = new SummaryGeneratingListener(); |
| 114 |
try { |
| 115 |
System.setProperty("running_within_test", "true"); |
| 116 |
final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() |
| 117 |
.selectors( |
| 118 |
testMethod == null ? |
| 119 |
selectClass(clazz) : |
| 120 |
selectMethod(clazz, testMethod) |
| 121 |
) |
| 122 |
.build(); |
| 123 |
|
| 124 |
LauncherFactory.create().execute(request, listener); |
| 125 |
return listener; |
| 126 |
} finally { |
| 127 |
System.clearProperty("running_within_test"); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
} |