assertj / assertj/assertj

Provide custom ErrorCollector

Open
#1,040 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.8k
Forks
788
Avg merge
14h 57m
Merged PRs (30d)
36

Description

#### Summary
I would like to extend the SoftProxies with custom ErrorCollector. Instead of doing actions on collected errors I want to have opportunity to perform collection based on the method name or arguments

#### Example
https://github.com/gauee/assertj-core/commit/40c4cc83d27ddb869a920ed42e039318c350541d
```java
public class ReportingErrorCollector extends ErrorCollector {
private static final Set METHODS_TO_SKIP_IN_LOGGING = ImmutableSet.of("as", "describedAs");

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Object result = super.intercept(obj, method, args, proxy);
if (obj instanceof AbstractAssert && !METHODS_TO_SKIP_IN_LOGGING.contains(method.getName())) {
AbstractAssert abstractAssert = (AbstractAssert) obj;
AssertionMessage assertionMessage = new AssertionMessage();
assertionMessage.setExpected(decorateExpected(args));
assertionMessage.setActual(decorateActual(abstractAssert));
assertionMessage.setComment(method.getName());
if (!wasSuccess()) {
assertionMessage.setFailed();
}
assertionMessage.log();
}
return result;
}

private static String decorateExpected(Object[] args) {
if (CollectionUtils.sizeIsEmpty(args)) {
return StringUtils.EMPTY;
}
return CollectionUtils.size(args) == 1 ? String.valueOf(args[0]) : Arrays.toString(args);
}

private static String decorateActual(AbstractAssert abstractAssert) {
return abstractAssert.actual == null ? StringUtils.EMPTY : abstractAssert.actual.toString();
}
}

```

#### Java 8 specific ?
* YES : create Pull Request from the `master` branch

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.