Run junit automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
package org.hamcrest.core;
import org.hamcrest.StringDescription;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.CombinableMatcher.both;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.junit.Assert.assertEquals;
public class CombinableTest {
private static final CombinableMatcher<Integer> EITHER_3_OR_4 = CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4));
private static final CombinableMatcher<Integer> NOT_3_AND_NOT_4 = CombinableMatcher.<Integer>both(not(equalTo(3))).and(not(equalTo(4)));
@Test
public void bothAcceptsAndRejects() {
assertThat(2, NOT_3_AND_NOT_4);
assertThat(3, not(NOT_3_AND_NOT_4));
}
@Test
public void acceptsAndRejectsThreeAnds() {
CombinableMatcher<? super Integer> tripleAnd = NOT_3_AND_NOT_4.and(equalTo(2));
assertThat(2, tripleAnd);
assertThat(3, not(tripleAnd));
}
@Test
public void bothDescribesItself() {
assertEquals("(not <3> and not <4>)", NOT_3_AND_NOT_4.toString());
StringDescription mismatch = new StringDescription();
NOT_3_AND_NOT_4.describeMismatch(3, mismatch);
assertEquals("was <3>", mismatch.toString());
}
@Test
public void eitherAcceptsAndRejects() {
assertThat(3, EITHER_3_OR_4);
assertThat(6, not(EITHER_3_OR_4));
}
@Test
public void acceptsAndRejectsThreeOrs() {
final CombinableMatcher<Integer> orTriple = EITHER_3_OR_4.or(greaterThan(10));
assertThat(11, orTriple);
assertThat(9, not(orTriple));
}
@Test
public void eitherDescribesItself() {
Assert.assertEquals("(<3> or <4>)", EITHER_3_OR_4.toString());
StringDescription mismatch = new StringDescription();
EITHER_3_OR_4.describeMismatch(6, mismatch);
Assert.assertEquals("was <6>", mismatch.toString());
}
@Test
public void picksUpTypeFromLeftHandSideOfExpression() {
assertThat("yellow", both(equalTo("yellow")).and(notNullValue()));
}
}
package org.hamcrest;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.AnyOf;
import org.hamcrest.core.CombinableMatcher;
import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher;
import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher;
import org.hamcrest.core.DescribedAs;
import org.hamcrest.core.Every;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsAnything;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsInstanceOf;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
import org.hamcrest.core.IsSame;
import org.hamcrest.core.StringContains;
import org.hamcrest.core.StringEndsWith;
import org.hamcrest.core.StringStartsWith;
public class CoreMatchers {
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
return AllOf.allOf((Iterable) matchers);
}
@SafeVarargs
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
return AllOf.allOf((Matcher[]) matchers);
}
public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
return AnyOf.anyOf((Iterable) matchers);
}
@SafeVarargs
public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
return AnyOf.anyOf((Matcher[]) matchers);
}
public static <LHS> CombinableBothMatcher<LHS> both(Matcher<? super LHS> matcher) {
return CombinableMatcher.both(matcher);
}
public static <LHS> CombinableEitherMatcher<LHS> either(Matcher<? super LHS> matcher) {
return CombinableMatcher.either(matcher);
}
public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values) {
return DescribedAs.describedAs(description, matcher, values);
}
public static <U> Matcher<Iterable<? extends U>> everyItem(Matcher<U> itemMatcher) {
return Every.everyItem(itemMatcher);
}
public static <T> Matcher<T> is(Matcher<T> matcher) {
return Is.is((Matcher) matcher);
}
public static <T> Matcher<T> is(T value) {
return Is.is((Object) value);
}
public static void is(Class<?> cls) {
}
public static <T> Matcher<T> isA(Class<T> type) {
return Is.isA(type);
}
public static Matcher<Object> anything() {
return IsAnything.anything();
}
public static Matcher<Object> anything(String description) {
return IsAnything.anything(description);
}
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) {
return IsCollectionContaining.hasItem((Matcher) itemMatcher);
}
public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
return IsCollectionContaining.hasItem((Object) item);
}
@SafeVarargs
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) {
return IsCollectionContaining.hasItems((Matcher[]) itemMatchers);
}
@SafeVarargs
public static <T> Matcher<Iterable<T>> hasItems(T... items) {
return IsCollectionContaining.hasItems((Object[]) items);
}
public static <T> Matcher<T> equalTo(T operand) {
return IsEqual.equalTo(operand);
}
public static Matcher<Object> equalToObject(Object operand) {
return IsEqual.equalToObject(operand);
}
public static <T> Matcher<T> any(Class<T> type) {
return IsInstanceOf.any(type);
}
public static <T> Matcher<T> instanceOf(Class<?> type) {
return IsInstanceOf.instanceOf(type);
}
public static <T> Matcher<T> not(Matcher<T> matcher) {
return IsNot.not((Matcher) matcher);
}
public static <T> Matcher<T> not(T value) {
return IsNot.not((Object) value);
}
public static Matcher<Object> notNullValue() {
return IsNull.notNullValue();
}
public static <T> Matcher<T> notNullValue(Class<T> type) {
return IsNull.notNullValue(type);
}
public static Matcher<Object> nullValue() {
return IsNull.nullValue();
}
public static <T> Matcher<T> nullValue(Class<T> type) {
return IsNull.nullValue(type);
}
public static <T> Matcher<T> sameInstance(T target) {
return IsSame.sameInstance(target);
}
public static <T> Matcher<T> theInstance(T target) {
return IsSame.theInstance(target);
}
public static Matcher<String> containsString(String substring) {
return StringContains.containsString(substring);
}
public static Matcher<String> containsStringIgnoringCase(String substring) {
return StringContains.containsStringIgnoringCase(substring);
}
public static Matcher<String> startsWith(String prefix) {
return StringStartsWith.startsWith(prefix);
}
public static Matcher<String> startsWithIgnoringCase(String prefix) {
return StringStartsWith.startsWithIgnoringCase(prefix);
}
public static Matcher<String> endsWith(String suffix) {
return StringEndsWith.endsWith(suffix);
}
public static Matcher<String> endsWithIgnoringCase(String suffix) {
return StringEndsWith.endsWithIgnoringCase(suffix);
}
}
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
public class CustomMatcher extends TypeSafeMatcher<CustomException> {
public static CustomMatcher hasCode(String item) {
return new CustomMatcher(item);
}
private String foundErrorCode;
private final String expectedErrorCode;
private CustomMatcher(String expectedErrorCode) {
this.expectedErrorCode = expectedErrorCode;
}
@Override
protected boolean matchesSafely(final CustomException exception) {
foundErrorCode = exception.getErrorCode();
return foundErrorCode.equalsIgnoreCase(expectedErrorCode);
}
@Override
public void describeTo(Description description) {
description.appendValue(foundErrorCode)
.appendText(" was not found instead of ")
.appendValue(expectedErrorCode);
}
}
import org.junit.rules.ExpectedException;
public class MyObjTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void someMethodThatThrowsCustomException() {
thrown.expect(CustomException.class);
thrown.expect(CustomMatcher.hasCode("110501"));
MyObj obj = new MyObj();
obj.methodThatThrowsCustomException();
}
}
thrown.expect(CombinableMatcher.both(
CoreMatchers.is(CoreMatchers.instanceOf(MyExceptionClass.class)))
.and(Matchers.hasProperty("errorCode", CoreMatchers.is(123))));
thrown.expect(CoreMatchers.instanceOf(MyExceptionClass.class));
thrown.expect(Matchers.hasProperty("errorCode", CoreMatchers.is(123));
@Test
public void whenSerialNumberIsEmpty_shouldThrowSerialNumberInvalid() throws Exception {
try{
whenRunningSomething_shouldThrowMyExceptionWithInternalErrorCode();
fail("should have thrown");
}
catch (MyExceptionClass e){
assertThat(e.getCode(), is(MyExceptionClass.INTERNAL_ERROR_CODE));
}
assertThat(BigDecimal.ONE,
is(both(not(nullValue()))
.and(not(comparesEqualTo(BigDecimal.ZERO)))));
^---The method and(Matcher<? super Object>) in the type
CombinableMatcher.CombinableBothMatcher<Object> is not
applicable for the arguments (Matcher<BigDecimal>)
assertThat(BigDecimal.ONE,
is(both(not(nullValue(BigDecimal.class)))
.and(not(comparesEqualTo(BigDecimal.ZERO)))));
assertThat(parentDecisionGroups, hasItem(
both(hasProperty("id", equalTo(decisionGroup1.getId()))).
and(hasProperty("ownerDecision", equalTo("decision1"))));
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTestās cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.