Best junit code snippet using org.junit.experimental.results.ResultMatchers.hasFailureContaining
Source:WithUnresolvedGenericTypeVariablesOnTheoryParms.java
1package org.junit.tests.experimental.theories.runner;2import static org.junit.Assert.assertThat;3import static org.junit.experimental.results.PrintableResult.testResult;4import static org.junit.experimental.results.ResultMatchers.failureCountIs;5import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;6import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;7import static org.junit.experimental.results.ResultMatchers.isSuccessful;8import java.util.Arrays;9import java.util.Collection;10import java.util.List;11import java.util.Map;12import org.junit.Test;13import org.junit.experimental.results.PrintableResult;14import org.junit.experimental.theories.DataPoint;15import org.junit.experimental.theories.DataPoints;16import org.junit.experimental.theories.Theories;17import org.junit.experimental.theories.Theory;18import org.junit.runner.RunWith;19public class WithUnresolvedGenericTypeVariablesOnTheoryParms {20 @Test21 public void whereTypeVariableIsOnTheTheory() {22 PrintableResult result = testResult(TypeVariableOnTheoryOnly.class);23 assertThat(result, isSuccessful());24 }25 @RunWith(Theories.class)26 public static class TypeVariableOnTheoryOnly {27 @DataPoint28 public static List<String> strings = Arrays.asList("foo", "bar");29 @Theory30 public <T> void forItems(Collection<?> items) {31 }32 }33 @Test34 public void whereTypeVariableIsOnTheoryParm() {35 PrintableResult result = testResult(TypeVariableOnTheoryParm.class);36 assertThat(result, hasSingleFailureContaining("unresolved type variable T"));37 }38 @RunWith(Theories.class)39 public static class TypeVariableOnTheoryParm {40 @DataPoint41 public static String string = "foo";42 @Theory43 public <T> void forItem(T item) {44 }45 }46 @Test47 public void whereTypeVariableIsOnParameterizedTheoryParm() {48 PrintableResult result = testResult(TypeVariableOnParameterizedTheoryParm.class);49 assertThat(result, hasSingleFailureContaining("unresolved type variable T"));50 }51 @RunWith(Theories.class)52 public static class TypeVariableOnParameterizedTheoryParm {53 @DataPoint54 public static List<String> strings = Arrays.asList("foo", "bar");55 @Theory56 public <T> void forItems(Collection<T> items) {57 }58 }59 @Test60 public void whereTypeVariableIsOnWildcardUpperBoundOnTheoryParm() {61 PrintableResult result = testResult(TypeVariableOnWildcardUpperBoundOnTheoryParm.class);62 assertThat(result, hasSingleFailureContaining("unresolved type variable U"));63 }64 @RunWith(Theories.class)65 public static class TypeVariableOnWildcardUpperBoundOnTheoryParm {66 @DataPoint67 public static List<String> strings = Arrays.asList("foo", "bar");68 @Theory69 public <U> void forItems(Collection<? extends U> items) {70 }71 }72 @Test73 public void whereTypeVariableIsOnWildcardLowerBoundOnTheoryParm() {74 PrintableResult result = testResult(TypeVariableOnWildcardLowerBoundOnTheoryParm.class);75 assertThat(result, hasSingleFailureContaining("unresolved type variable V"));76 }77 @RunWith(Theories.class)78 public static class TypeVariableOnWildcardLowerBoundOnTheoryParm {79 @DataPoint80 public static List<String> strings = Arrays.asList("foo", "bar");81 @Theory82 public <V> void forItems(Collection<? super V> items) {83 }84 }85 @Test86 public void whereTypeVariableIsOnArrayTypeOnTheoryParm() {87 PrintableResult result = testResult(TypeVariableOnArrayTypeOnTheoryParm.class);88 assertThat(result, hasSingleFailureContaining("unresolved type variable T"));89 }90 @RunWith(Theories.class)91 public static class TypeVariableOnArrayTypeOnTheoryParm {92 @DataPoints93 public static String[][] items() {94 return new String[][]{new String[]{"foo"}, new String[]{"bar"}};95 }96 @Theory97 public <T> void forItems(T[] items) {98 }99 }100 @Test101 public void whereTypeVariableIsOnComponentOfArrayTypeOnTheoryParm() {102 PrintableResult result = testResult(TypeVariableOnComponentOfArrayTypeOnTheoryParm.class);103 assertThat(result, hasSingleFailureContaining("unresolved type variable U"));104 }105 @RunWith(Theories.class)106 public static class TypeVariableOnComponentOfArrayTypeOnTheoryParm {107 @DataPoints108 public static List<?>[][] items() {109 return new List<?>[][]{110 new List<?>[]{Arrays.asList("foo")},111 new List<?>[]{Arrays.asList("bar")}112 };113 }114 @Theory115 public <U> void forItems(Collection<U>[] items) {116 }117 }118 @Test119 public void whereTypeVariableIsOnTheoryClass() {120 PrintableResult result = testResult(TypeVariableOnTheoryClass.class);121 assertThat(result, hasSingleFailureContaining("unresolved type variable T"));122 }123 @RunWith(Theories.class)124 public static class TypeVariableOnTheoryClass<T> {125 @DataPoint126 public static String item = "bar";127 @Theory128 public void forItem(T item) {129 }130 }131 @Test132 public void whereTypeVariablesAbound() {133 PrintableResult result = testResult(TypeVariablesAbound.class);134 assertThat(result, failureCountIs(7));135 assertThat(result, hasFailureContaining("unresolved type variable A"));136 assertThat(result, hasFailureContaining("unresolved type variable B"));137 assertThat(result, hasFailureContaining("unresolved type variable C"));138 assertThat(result, hasFailureContaining("unresolved type variable D"));139 assertThat(result, hasFailureContaining("unresolved type variable E"));140 assertThat(result, hasFailureContaining("unresolved type variable F"));141 assertThat(result, hasFailureContaining("unresolved type variable G"));142 }143 @RunWith(Theories.class)144 public static class TypeVariablesAbound<A, B extends A, C extends Collection<B>> {145 @Theory146 public <D, E extends D, F, G> void forItem(A first, Collection<B> second,147 Map<C, ? extends D> third, List<? super E> fourth, F[] fifth,148 Collection<G>[] sixth) {149 }150 }151}...
Source:VerifierRuleTest.java
...3import static org.hamcrest.CoreMatchers.is;4import static org.junit.Assert.assertEquals;5import static org.junit.Assert.assertThat;6import static org.junit.experimental.results.PrintableResult.testResult;7import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;8import static org.junit.experimental.results.ResultMatchers.isSuccessful;910import java.util.concurrent.Callable;1112import org.junit.Rule;13import org.junit.Test;14import org.junit.experimental.results.PrintableResult;15import org.junit.rules.ErrorCollector;16import org.junit.rules.Verifier;1718public class VerifierRuleTest {19 public static class UsesErrorCollector {20 @Rule21 public ErrorCollector collector= new ErrorCollector();22 23 @Test public void example() {24 collector.addError(new Throwable("message"));25 }26 }27 28 @Test public void usedErrorCollectorShouldFail() {29 assertThat(testResult(UsesErrorCollector.class), hasFailureContaining("message"));30 }31 32 public static class UsesErrorCollectorTwice {33 @Rule34 public ErrorCollector collector= new ErrorCollector();35 36 @Test public void example() {37 collector.addError(new Throwable("first thing went wrong"));38 collector.addError(new Throwable("second thing went wrong"));39 }40 }41 42 @Test public void usedErrorCollectorTwiceShouldFail() {43 PrintableResult testResult= testResult(UsesErrorCollectorTwice.class);44 assertThat(testResult, hasFailureContaining("first thing went wrong"));45 assertThat(testResult, hasFailureContaining("second thing went wrong"));46 }47 48 public static class UsesErrorCollectorCheckThat {49 @Rule50 public ErrorCollector collector= new ErrorCollector();51 52 @Test public void example() {53 collector.checkThat(3, is(4));54 collector.checkThat(5, is(6));55 }56 }57 58 @Test public void usedErrorCollectorCheckThatShouldFail() {59 PrintableResult testResult= testResult(UsesErrorCollectorCheckThat.class);60 assertThat(testResult, hasFailureContaining("got: <3>"));61 assertThat(testResult, hasFailureContaining("got: <5>"));62 }6364 public static class UsesErrorCollectorCheckSucceeds {65 @Rule66 public ErrorCollector collector= new ErrorCollector();67 68 @Test public void example() {69 collector.checkSucceeds(new Callable<Object>() {70 public Object call() throws Exception {71 throw new RuntimeException("first!");72 }73 });74 collector.checkSucceeds(new Callable<Object>() {75 public Object call() throws Exception {76 throw new RuntimeException("second!");77 }78 });79 }80 }81 82 @Test public void usedErrorCollectorCheckSucceedsShouldFail() {83 PrintableResult testResult= testResult(UsesErrorCollectorCheckSucceeds.class);84 assertThat(testResult, hasFailureContaining("first!"));85 assertThat(testResult, hasFailureContaining("second!"));86 }8788 public static class UsesErrorCollectorCheckSucceedsPasses {89 @Rule90 public ErrorCollector collector= new ErrorCollector();91 92 @Test public void example() {93 assertEquals(3, collector.checkSucceeds(new Callable<Object>() {94 public Object call() throws Exception {95 return 3;96 }97 }));98 }99 }
...
Source:UnsuccessfulWithDataPointFields.java
...3import static org.hamcrest.CoreMatchers.is;4import static org.junit.Assert.assertThat;5import static org.junit.experimental.results.PrintableResult.testResult;6import static org.junit.experimental.results.ResultMatchers.failureCountIs;7import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;8import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;9import static org.junit.matchers.JUnitMatchers.both;10import org.junit.Test;11import org.junit.experimental.theories.DataPoint;12import org.junit.experimental.theories.Theories;13import org.junit.experimental.theories.Theory;14import org.junit.runner.RunWith;15import org.junit.runners.model.TestClass;1617public class UnsuccessfulWithDataPointFields {18 @RunWith(Theories.class)19 public static class HasATheory {20 @DataPoint21 public static int ONE= 1;2223 @Theory24 public void everythingIsZero(int x) {25 assertThat(x, is(0));26 }27 }2829 @Test30 public void theoryClassMethodsShowUp() throws Exception {31 assertThat(new Theories(HasATheory.class).getDescription()32 .getChildren().size(), is(1));33 }3435 @Test36 public void theoryAnnotationsAreRetained() throws Exception {37 assertThat(new TestClass(HasATheory.class).getAnnotatedMethods(38 Theory.class).size(), is(1));39 }4041 @Test42 public void canRunTheories() throws Exception {43 assertThat(testResult(HasATheory.class),44 hasSingleFailureContaining("Expected"));45 }4647 @RunWith(Theories.class)48 public static class DoesntUseParams {49 @DataPoint50 public static int ONE= 1;5152 @Theory53 public void everythingIsZero(int x, int y) {54 assertThat(2, is(3));55 }56 }5758 @Test59 public void reportBadParams() throws Exception {60 assertThat(testResult(DoesntUseParams.class),61 hasSingleFailureContaining("everythingIsZero(ONE, ONE)"));62 }6364 @RunWith(Theories.class)65 public static class NullsOK {66 @DataPoint67 public static String NULL= null;6869 @DataPoint70 public static String A= "A";7172 @Theory73 public void everythingIsA(String a) {74 assertThat(a, is("A"));75 }76 }7778 @Test79 public void nullsUsedUnlessProhibited() throws Exception {80 assertThat(testResult(NullsOK.class),81 hasSingleFailureContaining("null"));82 }8384 @RunWith(Theories.class)85 public static class DataPointsMustBeStatic {86 @DataPoint87 int THREE= 3;8889 @DataPoint90 int FOUR= 3;9192 @Theory93 public void numbers(int x) {9495 }96 }9798 @Test99 public void dataPointsMustBeStatic() {100 assertThat(101 testResult(DataPointsMustBeStatic.class),102 both(failureCountIs(2))103 .and(104 hasFailureContaining("DataPoint field THREE must be static"))105 .and(106 hasFailureContaining("DataPoint field FOUR must be static")));107 }108109 @RunWith(Theories.class)110 public static class TheoriesMustBePublic {111 @DataPoint112 public static int THREE= 3;113114 @Theory115 void numbers(int x) {116117 }118 }119120 @Test
...
Source:ResultMatchersTest.java
...21 assertThat(ResultMatchers.failureCountIs(i).toString(),22 containsString("" + i));23 }24 @Test25 public void hasFailureContaining_givenResultWithNoFailures() {26 PrintableResult resultWithNoFailures = new PrintableResult(new ArrayList<Failure>());27 assertThat(ResultMatchers.hasFailureContaining("").matches(resultWithNoFailures), is(false));28 }29 @Test30 public void hasFailureContaining_givenResultWithOneFailure() {31 PrintableResult resultWithOneFailure = new PrintableResult(Collections.singletonList(32 new Failure(Description.EMPTY, new RuntimeException("my failure"))));33 assertThat(ResultMatchers.hasFailureContaining("my failure").matches(resultWithOneFailure), is(true));34 assertThat(ResultMatchers.hasFailureContaining("his failure").matches(resultWithOneFailure), is(false));35 }36}...
hasFailureContaining
Using AI Code Generation
1import static org.junit.experimental.results.PrintableResult.*;2import static org.junit.experimental.results.ResultMatchers.*;3import static org.junit.runner.Description.*;4import org.junit.Test;5import org.junit.runner.RunWith;6import org.junit.runners.JUnit4;7@RunWith(JUnit4.class)8public class JUnit4Test {9 public void test() {10 assertThat(testResult(FailingTest.class), hasFailureContaining("expected"));11 }12 public static class FailingTest {13 @Test public void fail() {14 fail("expected");15 }16 }17}
hasFailureContaining
Using AI Code Generation
1import static org.junit.experimental.results.PrintableResult.testResult;2import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;3import org.junit.Test;4public class TestJUnitResultMatcher {5 public void testFailures() {6 org.junit.runner.Result result = testResult(FailingTest.class);7 assertThat(result, hasFailureContaining("expected:<[tru]e> but was:<[fals]e>"));8 }9}10public class FailingTest {11 public void test() {12 assertThat(1 + 1, is(3));13 }14}15 at org.junit.Assert.assertEquals(Assert.java:115)16 at org.junit.Assert.assertEquals(Assert.java:144)17 at org.junit.Assert.assertThat(Assert.java:780)18 at org.junit.Assert.assertThat(Assert.java:738)19 at org.junit.experimental.results.PrintableResultTest$FailingTest.test(PrintableResultTest.java:22)20import static org.hamcrest.MatcherAssert.assertThat;21import static org.hamcrest.Matchers.is;22import org.junit.jupiter.api.Test;23public class TestJUnit5Assertion {24 public void testAssertion() {25 assertThat(1 + 1, is(2));26 }27}28import static org.junit.platform.console.tasks.ResultAssertions.printResult;29import org.junit.platform.engine.TestExecutionResult;30import org.junit.platform.engine.support.descriptor.EngineDescriptor;31import org.junit.platform.launch
hasFailureContaining
Using AI Code Generation
1ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")2ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")3ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")4ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")5ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")6ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")7ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")8ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")9ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")10ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")11ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")12ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")13ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")14ResultMatchers.hasFailureContaining("Expected: is \"foo\" but: was \"bar\"")
hasFailureContaining
Using AI Code Generation
1public class JUnitResultMatchersTest {2 public void testFailure() {3 Result result = JUnitCore.runClasses(FailureTest.class);4 assertThat(result, hasFailureContaining("Expected"));5 }6}7 at org.junit.Assert.assertThat(Assert.java:780)8 at org.junit.Assert.assertThat(Assert.java:738)9 at org.junit.experimental.results.PrintableResultTest.testFailure(PrintableResultTest.java:33)10 at org.junit.Assert.assertThat(Assert.java:780)11 at org.junit.Assert.assertThat(Assert.java:738)12 at org.junit.experimental.results.PrintableResultTest.testFailure(PrintableResultTest.java:33)13 at org.junit.Assert.assertThat(Assert.java:780)14 at org.junit.Assert.assertThat(Assert.java:738)15 at org.junit.experimental.results.PrintableResultTest.testFailure(PrintableResultTest.java:33)16 at org.junit.Assert.assertThat(Assert.java:780)17 at org.junit.Assert.assertThat(Assert.java:738)18 at org.junit.experimental.results.PrintableResultTest.testFailure(PrintableResultTest.java:33)19 at org.junit.Assert.assertThat(Assert.java:780)20 at org.junit.Assert.assertThat(Assert.java:738)21 at org.junit.experimental.results.PrintableResultTest.testFailure(PrintableResultTest.java:33)
hasFailureContaining
Using AI Code Generation
1class TestClass {2 void test() {3 fail("failure message");4 }5}6class TestRunner {7 public static void main(String... args) {8 Result result = JUnitCore.runClasses(TestClass.class);9 System.out.println(result.wasSuccessful());10 }11}12Result result = JUnitCore.runClasses(TestClass.class);13assertThat(result, hasFailureContaining("failure message"));14Result result = JUnitCore.runClasses(TestClass.class);15assertThat(result, hasFailureContaining("invalid message"));16Result result = JUnitCore.runClasses(TestClass.class);17assertThat(result, hasFailureContaining(".*failure.*"));18Result result = JUnitCore.runClasses(TestClass.class);19assertThat(result, hasFailureContaining(".*invalid.*"));20Result result = JUnitCore.runClasses(TestClass.class);21assertThat(result, hasFailureContaining(".*failure.*"));22Result result = JUnitCore.runClasses(TestClass.class);23assertThat(result, hasFailureContaining(".*invalid.*"));24Result result = JUnitCore.runClasses(TestClass.class);25assertThat(result, hasFailureContaining(".*invalid.*"));
hasFailureContaining
Using AI Code Generation
1import org.junit.Test2import kotlin.test.assertEquals3class CalculatorTest {4 fun `should add two numbers`() {5 val calculator = Calculator()6 val result = calculator.add(1, 2)7 assertEquals(3, result)8 }9}10import org.junit.jupiter.api.Assertions.assertEquals11import org.junit.jupiter.api.Test12class CalculatorTest {13 fun `should add two numbers`() {14 val calculator = Calculator()15 val result = calculator.add(1, 2)16 assertEquals(3, result)17 }18}19import org.junit.Assert.assertEquals20import org.junit.Test21class CalculatorTest {22 fun `should add two numbers`() {23 val calculator = Calculator()24 val result = calculator.add(1, 2)25 assertEquals(3, result)26 }27}
hasFailureContaining
Using AI Code Generation
1import org.junit.Test;2import org.junit.experimental.results.ResultMatchers;3import org.junit.runner.JUnitCore;4public class TestClass {5 public void test() {6 ResultMatchers.hasFailureContaining("test");7 }8}9org.junit.experimental.results.ResultMatchers has no method hasFailureContaining(java.lang.String)10import org.junit.Test;11import org.hamcrest.MatcherAssert;12import org.hamcrest.Matchers;13public class TestClass {14 public void test() {15 MatcherAssert.assertThat("test", Matchers.hasFailureContaining("test"));16 }17}18import org.junit.Test;19import org.hamcrest.MatcherAssert;20import org.hamcrest.Matchers;21public class TestClass {22 public void test() {23 MatcherAssert.assertThat("test", Matchers.hasFailureContaining("test"));24 }25}
hasFailureContaining
Using AI Code Generation
1public void testForFailure() throws Exception {2 Result result = JUnitCore.runClasses(ExampleTest.class);3 assertThat(result, hasFailureContaining("failure"));4}5package com.baeldung.junit.result;6import org.junit.Test;7import static org.junit.Assert.fail;8public class ExampleTest {9 public void testForFailure() {10 fail("failure");11 }12}13package org.junit.experimental.results;14import org.hamcrest.Description;15import org.hamcrest.Factory;16import org.hamcrest.Matcher;17import org.hamcrest.TypeSafeMatcher;18import org.junit.runner.Result;19public class ResultMatchers {20 public static Matcher<Result> hasFailureContaining(final String substring) {21 return new TypeSafeMatcher<Result>() {22 public void describeTo(Description description) {23 description.appendText("has failure containing " + substring);24 }25 protected boolean matchesSafely(Result result) {26 return result.getFailures().get(0).toString().contains(substring);27 }28 };29 }30}31package org.junit.experimental.results;32import org.junit.Test;33import static org.junit.Assert.fail;34import static org.junit.experimental.results.PrintableResult.testResult;35import static org.junit.experimental.results.ResultMatchers.isSuccessful;36import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;37import static org.junit.Assert.assertThat;38public class ResultMatchersTest {39 public void successful() {40 assertThat(testResult(Success.class), isSuccessful());41 }42 public void failing() {43 assertThat(testResult(Failure.class), hasFailureContaining("failure"));44 }45 public static class Success {46 public void succeed() {47 }48 }49 public static class Failure {50 public void fail() {51 fail("failure");52 }53 }54}55package org.junit.experimental.results;56import java.io.ByteArrayOutputStream;57import java.io.PrintStream;58import org.junit.runner.JUnitCore;59import org.junit.runner.Result;60public class PrintableResult {61 public static Result testResult(Class<?>... classes) {62 return JUnitCore.runClasses(classes);63 }64 public static String testResultString(Class<?>... classes) {65 Result result = testResult(classes);66 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!