Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extended stack trace filtering (#873)
- remove assertj related elements from stack trace of error thrown by failWithMessage(...) - also remove all elements of custom assert subclasses
- Loading branch information
1 parent
c7d3221
commit 00b5540
Showing
4 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/org/example/custom/CustomAsserts_filter_stacktrace_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2017 the original author or authors. | ||
*/ | ||
package org.example.custom; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.assertj.core.api.AbstractObjectAssert; | ||
import org.assertj.core.api.Condition; | ||
import org.assertj.core.error.BasicErrorMessageFactory; | ||
import org.assertj.core.internal.Failures; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CustomAsserts_filter_stacktrace_Test { | ||
|
||
@Test | ||
public void should_filter_when_custom_assert_fails_with_message() { | ||
try { | ||
new CustomAssert("").fail(); | ||
} catch (AssertionError e) { | ||
assertThat(e.getStackTrace()).areNot(elementOf(CustomAssert.class)); | ||
} | ||
} | ||
|
||
@Test | ||
public void should_filter_when_custom_assert_fails_with_overridden_message() { | ||
try { | ||
new CustomAssert("").overridingErrorMessage("overridden message").fail(); | ||
} catch (AssertionError e) { | ||
assertThat(e.getStackTrace()).areNot(elementOf(CustomAssert.class)); | ||
} | ||
} | ||
|
||
@Test | ||
public void should_filter_when_custom_assert_throws_assertion_error() { | ||
try { | ||
new CustomAssert("").throwAnAssertionError(); | ||
} catch (AssertionError e) { | ||
assertThat(e.getStackTrace()).areNot(elementOf(CustomAssert.class)); | ||
} | ||
} | ||
|
||
@Test | ||
public void should_filter_when_abstract_custom_assert_fails() { | ||
try { | ||
new CustomAssert("").failInAbstractAssert(); | ||
} catch (AssertionError e) { | ||
assertThat(e.getStackTrace()).areNot(elementOf(CustomAbstractAssert.class)); | ||
} | ||
} | ||
|
||
@Test | ||
public void should_not_filter_when_global_remove_option_is_disabled() { | ||
Failures.instance().setRemoveAssertJRelatedElementsFromStackTrace(false); | ||
try { | ||
new CustomAssert("").fail(); | ||
} catch (AssertionError e) { | ||
assertThat(e.getStackTrace()).areAtLeastOne(elementOf(CustomAssert.class)); | ||
} | ||
} | ||
|
||
@Before | ||
@After | ||
public void enableStackTraceFiltering() { | ||
Failures.instance().setRemoveAssertJRelatedElementsFromStackTrace(true); | ||
} | ||
|
||
private static Condition<StackTraceElement> elementOf(final Class<?> clazz) { | ||
return new Condition<StackTraceElement>("StackTraceElement of " + clazz) { | ||
@Override | ||
public boolean matches(StackTraceElement value) { | ||
return value.getClassName().equals(clazz.getName()); | ||
} | ||
}; | ||
} | ||
|
||
private static class CustomAssert extends CustomAbstractAssert<CustomAssert> { | ||
|
||
public CustomAssert(String actual) { | ||
super(actual, CustomAssert.class); | ||
} | ||
|
||
public CustomAssert fail() { | ||
failWithMessage("failing assert"); | ||
return this; | ||
} | ||
|
||
public CustomAssert throwAnAssertionError() { | ||
throwAssertionError(new BasicErrorMessageFactory("failing assert")); | ||
return this; | ||
} | ||
} | ||
|
||
private static class CustomAbstractAssert<S extends CustomAbstractAssert<S>> extends AbstractObjectAssert<S, String> { | ||
|
||
protected CustomAbstractAssert(String actual, Class<?> selfType) { | ||
super(actual, selfType); | ||
} | ||
|
||
public S failInAbstractAssert() { | ||
failWithMessage("failing assert"); | ||
return myself; | ||
} | ||
} | ||
} |