Replies: 1 comment 1 reply
-
LazyParams offers ways to apply ad-hoc parametrization "on top of all existing" ... import org.apache.log4j.Level; //Using Log4j, because I didn't figure out which flavor ...
import org.apache.log4j.Logger;//... of Slf4j was used in the original extension example.
import org.lazyparams.LazyParams;
import org.lazyparams.showcase.ToPick;
public class LogLevelExtension implements BeforeEachCallback {
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface LogLevel {
String[] loggers();
String level();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(LogLevelExtension.class)
public @interface LogLevels {
LogLevel[] value();
}
private LogLevels[] extractedLogLevels(final Method method) {
final Class<?> parentClass = method.getDeclaringClass();
final LogLevels[] classLevelAnnotations = parentClass.getAnnotationsByType(LogLevels.class);
final List<LogLevels> cl = Arrays.asList(classLevelAnnotations);
// log.debug("Found {} log level annotations '{}' for method '{}' class '{}'", cl.size(), cl, method.getName(), parentClass.getName());
final LogLevels[] methodLevelAnnotations = method.getAnnotationsByType(LogLevels.class);
final List<LogLevels> ml = Arrays.asList(methodLevelAnnotations);
// log.debug("Found {} log level annotations '{}' for method '{}'", ml.size(), ml, method.getName());
final Set<LogLevels> r = new LinkedHashSet<>();
r.addAll(cl);
r.addAll(ml);
// log.debug("Found {} log level annotations '{}' for '{}'", r.size(), r, method.getName());
return r.toArray(new LogLevels[0]);
}
@Override
public void beforeEach(final ExtensionContext context) {
final LogLevels[] levelsOptions = extractedLogLevels(context.getRequiredTestMethod());
if (0 == levelsOptions.length) {
return; /* No parameter values*/
}
final LogLevel logLevel = Stream.of(levelsOptions)
.map(LogLevels::value).flatMap(Stream::of)
.collect(ToPick.from())
.asParameter(LogLevel::level)//Specify test display-name appendix!
.pickValue();//Pick value and therewith enforce parametrization!
final Level level = Level.toLevel(logLevel.level().toUpperCase());
final Map<String, Level> originalLevels = new HashMap<>();
for (final String loggerName : logLevel.loggers()) {
final Logger logger = (Logger) Logger.getLogger(loggerName);
originalLevels.put(loggerName, logger.getLevel());
logger.setLevel(level);
}
/* Setup retirement-plan that restores original levels: */
LazyParams.currentScopeConfiguration().setScopedCustomItem(
new Object(), originalLevels, this::afterRestore);
}
private void afterRestore(Map<String,Level> originalLevels) {
for (final Map.Entry<String, Level> entry : originalLevels.entrySet()) {
final Logger logger = (Logger) Logger.getLogger(entry.getKey());
logger.setLevel(entry.getValue());
}
}
} E.g. if above enum FooData {
foo, fuu, fyy;
} ... then ConsoleLauncher could present this result for your test-class
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is my extension. I made this because in my business code I have lot of "if logger.isTrace() then trace else debug" ... And I need to have all code covered by tests.
But now I face a problem: I can't combine my new extension with already existing RepeatedTest and ParameterizedTest.
This is my test:
The problem is now that I can't combine my own LogLevelTestTemplateInvocationContextProvider extension with the ParameterizedTest extension.
So I found only a workaround to skip my extension for tests which are already annotated with ParameterizedTest or RepeatedTest.
But what I really want is my own extension which can combined with these 2 extensions.
I would like to have something like "on top of all existing" TestTemplate-Extensions ... so that I can' create even more test's based on the previously created ones .... like another loop over all calculated TestTemplateInvocationContext's or lets call it a "post-processor" for TestTemplateInvocationContext's.
Hope its understandable :)
Kind regards
Andreas
Beta Was this translation helpful? Give feedback.
All reactions