...5import static org.junit.Assert.assertSame;6import static org.junit.Assert.assertThat;7import static org.junit.Assert.assertTrue;8import static org.junit.Assert.fail;9import static org.junit.Assume.assumeNoException;10import static org.junit.Assume.assumeNotNull;11import static org.junit.Assume.assumeThat;12import static org.junit.Assume.assumeTrue;13import static org.junit.experimental.results.PrintableResult.testResult;14import static org.junit.experimental.results.ResultMatchers.isSuccessful;15import java.util.ArrayList;16import java.util.List;17import org.junit.Assume;18import org.junit.Before;19import org.junit.BeforeClass;20import org.junit.Test;21import org.junit.internal.AssumptionViolatedException;22import org.junit.runner.JUnitCore;23import org.junit.runner.Result;24import org.junit.runner.notification.Failure;25import org.junit.runner.notification.RunListener;26public class AssumptionTest {27 public static class HasFailingAssumption {28 @Test29 public void assumptionsFail() {30 assumeThat(3, is(4));31 fail();32 }33 }34 @Test35 public void failedAssumptionsMeanPassing() {36 Result result = JUnitCore.runClasses(HasFailingAssumption.class);37 assertThat(result.getRunCount(), is(1));38 assertThat(result.getIgnoreCount(), is(0));39 assertThat(result.getFailureCount(), is(0));40 }41 private static int assumptionFailures = 0;42 @Test43 public void failedAssumptionsCanBeDetectedByListeners() {44 assumptionFailures = 0;45 JUnitCore core = new JUnitCore();46 core.addListener(new RunListener() {47 @Override48 public void testAssumptionFailure(Failure failure) {49 assumptionFailures++;50 }51 });52 core.run(HasFailingAssumption.class);53 assertThat(assumptionFailures, is(1));54 }55 public static class HasPassingAssumption {56 @Test57 public void assumptionsFail() {58 assumeThat(3, is(3));59 fail();60 }61 }62 @Test63 public void passingAssumptionsScootThrough() {64 Result result = JUnitCore.runClasses(HasPassingAssumption.class);65 assertThat(result.getRunCount(), is(1));66 assertThat(result.getIgnoreCount(), is(0));67 assertThat(result.getFailureCount(), is(1));68 }69 @Test(expected = AssumptionViolatedException.class)70 public void assumeThatWorks() {71 assumeThat(1, is(2));72 }73 @Test74 public void assumeThatPasses() {75 assumeThat(1, is(1));76 assertCompletesNormally();77 }78 @Test79 public void assumeThatPassesOnStrings() {80 assumeThat("x", is("x"));81 assertCompletesNormally();82 }83 @Test(expected = AssumptionViolatedException.class)84 public void assumeNotNullThrowsException() {85 Object[] objects = {1, 2, null};86 assumeNotNull(objects);87 }88 @Test89 public void assumeNotNullPasses() {90 Object[] objects = {1, 2};91 assumeNotNull(objects);92 assertCompletesNormally();93 }94 @Test95 public void assumeNotNullIncludesParameterList() {96 try {97 Object[] objects = {1, 2, null};98 assumeNotNull(objects);99 } catch (AssumptionViolatedException e) {100 assertThat(e.getMessage(), containsString("1, 2, null"));101 } catch (Exception e) {102 fail("Should have thrown AssumptionViolatedException");103 }104 }105 @Test106 public void assumeNoExceptionThrows() {107 final Throwable exception = new NullPointerException();108 try {109 assumeNoException(exception);110 fail("Should have thrown exception");111 } catch (AssumptionViolatedException e) {112 assertThat(e.getCause(), is(exception));113 }114 }115 private void assertCompletesNormally() {116 }117 @Test(expected = AssumptionViolatedException.class)118 public void assumeTrueWorks() {119 Assume.assumeTrue(false);120 }121 public static class HasFailingAssumeInBefore {122 @Before123 public void checkForSomethingThatIsntThere() {124 assumeTrue(false);125 }126 @Test127 public void failing() {128 fail();129 }130 }131 @Test132 public void failingAssumptionInBeforePreventsTestRun() {133 assertThat(testResult(HasFailingAssumeInBefore.class), isSuccessful());134 }135 public static class HasFailingAssumeInBeforeClass {136 @BeforeClass137 public static void checkForSomethingThatIsntThere() {138 assumeTrue(false);139 }140 @Test141 public void failing() {142 fail();143 }144 }145 @Test146 public void failingAssumptionInBeforeClassIgnoresClass() {147 assertThat(testResult(HasFailingAssumeInBeforeClass.class), isSuccessful());148 }149 public static class AssumptionFailureInConstructor {150 public AssumptionFailureInConstructor() {151 assumeTrue(false);152 }153 @Test154 public void shouldFail() {155 fail();156 }157 }158 @Test159 public void failingAssumptionInConstructorIgnoresClass() {160 assertThat(testResult(AssumptionFailureInConstructor.class), isSuccessful());161 }162 @Test(expected = IllegalArgumentException.class)163 public void assumeWithExpectedException() {164 assumeTrue(false);165 }166 final static String message = "Some random message string.";167 final static Throwable t = new Throwable();168 /**169 * @see AssumptionTest#assumptionsWithMessage()170 */171 public static class HasAssumeWithMessage {172 @Test173 public void testMethod() {174 assumeTrue(message, false);175 }176 }177 @Test178 public void assumptionsWithMessage() {179 final List<Failure> failures =180 runAndGetAssumptionFailures(HasAssumeWithMessage.class);181 assertTrue(failures.get(0).getMessage().contains(message));182 }183 /**184 * @see AssumptionTest#assumptionsWithMessageAndCause()185 */186 public static class HasAssumeWithMessageAndCause {187 @Test188 public void testMethod() {189 assumeNoException(message, t);190 }191 }192 @Test193 public void assumptionsWithMessageAndCause() {194 final List<Failure> failures =195 runAndGetAssumptionFailures(HasAssumeWithMessageAndCause.class);196 assertTrue(failures.get(0).getMessage().contains(message));197 assertSame(failures.get(0).getException().getCause(), t);198 }199 public static class HasFailingAssumptionWithMessage {200 @Test201 public void assumptionsFail() {202 assumeThat(message, 3, is(4));203 fail();204 }205 }206 @Test207 public void failedAssumptionsWithMessage() {208 final List<Failure> failures =209 runAndGetAssumptionFailures(HasFailingAssumptionWithMessage.class);...