Skip to content

Commit

Permalink
Issue-821: Fix logging errors during annotation reading
Browse files Browse the repository at this point in the history
In commit fd97825 a catch clause was removed that omitted errors from the annotation evaluator that were from reading annotations that simply don't have a value method, because they were never meant to be tags.
This results in JGiven printing lots of errors for every annation that could possbliy exist on a test class or method.

Therefore the catch clause was reintroduced an a test for its existence was added.

Signed-off-by: l-1sqared <[email protected]>
  • Loading branch information
l-1squared committed Feb 1, 2022
1 parent 0c43f19 commit d5b4271
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v1.2x
##New features
* The `@Tag` annotation configuration can now be set differently from the JGiven defaults. [#806](https://github.com/TNG/JGiven/issues/806)
##Fixed issues
* JGiven no longer prints errors while scanning annotations that are not tags. [#821](https://github.com/TNG/JGiven/issues/821)
##Backward incompatible changes
# v1.2
## New features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private Optional<Object> getValuesFromAnnotation(Annotation annotation) {
try {
Method method = annotation.annotationType().getMethod("value");
return Optional.ofNullable(method.invoke(annotation));
} catch (NoSuchMethodException ignoreAnnotationsThatAreNotTags) {
return Optional.empty();
} catch (Exception e) {
log.error("Error while getting 'value' method of annotation " + annotation, e);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.tngtech.jgiven.config.DefaultConfiguration;
import com.tngtech.jgiven.impl.TestUtil.JGivenLogHandler;
import com.tngtech.jgiven.report.model.Tag;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Test;

public class TagCreatorTest {

private final TagCreator underTest = new TagCreator(new DefaultConfiguration());
private final JGivenLogHandler interceptor = new JGivenLogHandler();

@Before
public void addLogInterceptor() {
Logger testLogger = LogManager.getLogManager().getLogger(TagCreator.class.getName());
testLogger.addHandler(interceptor);
}

@Test
public void testAnnotationParsing() {
Tag tag = getOnlyTagFor(AnnotationTestClass.class.getAnnotations()[0]);
assertThat(tag.getName()).isEqualTo("AnnotationWithoutValue");
assertThat(tag.getValues()).isEmpty();
assertThat(interceptor.containsLoggingEvent(record -> record.getLevel() == Level.SEVERE))
.as("Attempt to convert an annotation without value method results in an error log")
.isFalse();
}

@Test
Expand Down

0 comments on commit d5b4271

Please sign in to comment.