Skip to content

Commit

Permalink
Configure visual test separator using annotation values
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys committed Mar 4, 2025
1 parent 320fa1b commit de14651
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -21,6 +22,20 @@
*/
@Target(ElementType.TYPE)
@Retention(RUNTIME)
@Inherited
@ExtendWith(TestVisualSeparatorExtension.class)
public @interface TestVisualSeparator {
/**
* Sets separator char
*
* @return visual separator char
*/
String separator() default "#";

/**
* Sets separator length
*
* @return length of visual separator line
*/
int lineLength() default 76;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package io.skodjob.testframe.listeners;

import io.skodjob.testframe.annotations.TestVisualSeparator;
import io.skodjob.testframe.utils.LoggerUtils;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
Expand All @@ -13,6 +14,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;

/**
* jUnit5 specific class which listening on test callbacks
*/
Expand All @@ -26,19 +31,19 @@ private TestVisualSeparatorExtension() {

@Override
public void beforeAll(ExtensionContext extensionContext) {
LoggerUtils.logSeparator();
logSeparator(extensionContext);
LOGGER.info("TestClass {} STARTED", extensionContext.getRequiredTestClass().getName());
}

@Override
public void afterAll(ExtensionContext extensionContext) {
LOGGER.info("TestClass {} FINISHED", extensionContext.getRequiredTestClass().getName());
LoggerUtils.logSeparator();
logSeparator(extensionContext);
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
LoggerUtils.logSeparator();
logSeparator(extensionContext);
LOGGER.info("Test {}.{} STARTED", extensionContext.getRequiredTestClass().getName(),
extensionContext.getDisplayName().replace("()", ""));
}
Expand All @@ -52,6 +57,16 @@ public void afterEach(ExtensionContext extensionContext) {

LOGGER.info("Test {}.{} {}", extensionContext.getRequiredTestClass().getName(),
extensionContext.getDisplayName().replace("()", ""), state);
LoggerUtils.logSeparator();
logSeparator(extensionContext);
}

private void logSeparator(ExtensionContext extensionContext) {
Optional<TestVisualSeparator> annotation =
findAnnotation(extensionContext.getRequiredTestClass(), TestVisualSeparator.class);
if (annotation.isPresent()) {
LoggerUtils.logSeparator(annotation.get().separator(), annotation.get().lineLength());
} else {
LoggerUtils.logSeparator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

@ResourceManager
@TestVisualSeparator
@TestVisualSeparator(separator = "=")
public abstract class AbstractIT {
static AtomicBoolean isCreateHandlerCalled = new AtomicBoolean(false);
static AtomicBoolean isDeleteHandlerCalled = new AtomicBoolean(false);
Expand Down

0 comments on commit de14651

Please sign in to comment.