com.codereligion.cherry.test.hamcrest.logback
Class LoggingEventHasThrowable

java.lang.Object
  extended by org.hamcrest.BaseMatcher<T>
      extended by org.hamcrest.TypeSafeMatcher<ch.qos.logback.classic.spi.ILoggingEvent>
          extended by com.codereligion.cherry.test.hamcrest.logback.LoggingEventHasThrowable
All Implemented Interfaces:
org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent>, org.hamcrest.SelfDescribing

public class LoggingEventHasThrowable
extends org.hamcrest.TypeSafeMatcher<ch.qos.logback.classic.spi.ILoggingEvent>

A matcher which expects the ILoggingEvent to contain the specified Throwable.

This matcher returns true when:

Since:
17.03.2015
Author:
Sebastian Gröbler

Method Summary
protected  void describeExpectation(org.hamcrest.Description description)
          Describes the expected data.
protected  void describeMismatchSafely(ch.qos.logback.classic.spi.ILoggingEvent item, org.hamcrest.Description mismatchDescription)
           
protected  void describeNegatedExpectation(org.hamcrest.Description description)
          Describes the expected data when the matcher has been negated.
 void describeTo(org.hamcrest.Description description)
           
static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> doesNotHaveThrowable(Throwable throwable)
          Creates a new matcher for ILoggingEvents that only matches when the examined event does not have a Throwable associated to it which matches the given one.
static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> hasThrowable(Throwable throwable)
          Creates a new matcher for ILoggingEvents that only matches when the examined event has a Throwable associated to it which matches the given one.
protected  boolean internalMatches(ch.qos.logback.classic.spi.ILoggingEvent event)
          This method must return true when the given event matches.
 boolean matchesSafely(ch.qos.logback.classic.spi.ILoggingEvent event)
           
static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> withThrowable(Throwable throwable)
          Creates a new matcher for ILoggingEvents that only matches when the examined event has a Throwable associated to it which matches the given one.
 
Methods inherited from class org.hamcrest.TypeSafeMatcher
describeMismatch, matches
 
Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

hasThrowable

public static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> hasThrowable(Throwable throwable)
Creates a new matcher for ILoggingEvents that only matches when the examined event has a Throwable associated to it which matches the given one. This matcher is doing the same assertion as withThrowable(Throwable), with the difference that this matcher's output is optimized for usage on single events.

Example usage: assertThat(event, hasThrowable(new NullPointerException("opsi!)));

Example output: Expected: an ILoggingEvent with a throwable matching: java.lang.NullPointerException{message='opsi!'} but: was ILoggingEvent{level=ERROR, formattedMessage='some Message', loggedBy=SomeLogger, throwable=java.lang.NullPointerException{message='nope!'}}

Parameters:
throwable - the throwable to match the event's associated throwable with
Returns:
a new matcher

doesNotHaveThrowable

public static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> doesNotHaveThrowable(Throwable throwable)
Creates a new matcher for ILoggingEvents that only matches when the examined event does not have a Throwable associated to it which matches the given one. This matcher is the negation of hasThrowable(Throwable). It is recommended to use this specific matcher instead of just combining the other matcher with CoreMatchers.not(Matcher) because of the improved error output.

Example usage: assertThat(event, doesNotHaveThrowable(new NullPointerException("ohoh")));

Example output: Expected: an ILoggingEvent with a throwable not matching: java.lang.NullPointerException{message='ohoh'} but: was ILoggingEvent{level=ERROR, formattedMessage='some message', loggedBy=SomeLogger, throwable=java.lang.NullPointerException{message='ohoh'}}

Parameters:
throwable - the throwable to match the event's associated throwable with
Returns:
a new matcher

withThrowable

public static org.hamcrest.Matcher<ch.qos.logback.classic.spi.ILoggingEvent> withThrowable(Throwable throwable)
Creates a new matcher for ILoggingEvents that only matches when the examined event has a Throwable associated to it which matches the given one. This matcher is doing the same assertion as hasThrowable(Throwable), with the difference that this matcher's output is optimized for usage on iterables of events.

Example usage: assertThat(events, hasItem(withThrowable(new NullPointerException("ohoh"))));

Example output: Expected: an iterable containing an ILoggingEvent with a throwable matching: java.lang.NullPointerException{message='ohoh'} but: iterable contained [ILoggingEvent{level=INFO, formattedMessage='some Message', loggedBy=SomeLogger, throwable=null}]

Parameters:
throwable - the throwable to match the event's associated throwable with
Returns:
a new matcher

internalMatches

protected boolean internalMatches(ch.qos.logback.classic.spi.ILoggingEvent event)
This method must return true when the given event matches. Proper negation is handled by the caller of this method.

Parameters:
event - the ILoggingEvent to be checked
Returns:
true if the event matches

describeExpectation

protected void describeExpectation(org.hamcrest.Description description)
Describes the expected data.

Parameters:
description - the Description to amend the text

describeNegatedExpectation

protected void describeNegatedExpectation(org.hamcrest.Description description)
Describes the expected data when the matcher has been negated.

Parameters:
description - the Description to amend the text

matchesSafely

public boolean matchesSafely(ch.qos.logback.classic.spi.ILoggingEvent event)
Specified by:
matchesSafely in class org.hamcrest.TypeSafeMatcher<ch.qos.logback.classic.spi.ILoggingEvent>

describeTo

public void describeTo(org.hamcrest.Description description)

describeMismatchSafely

protected void describeMismatchSafely(ch.qos.logback.classic.spi.ILoggingEvent item,
                                      org.hamcrest.Description mismatchDescription)
Overrides:
describeMismatchSafely in class org.hamcrest.TypeSafeMatcher<ch.qos.logback.classic.spi.ILoggingEvent>


Copyright © 2015. All rights reserved.