Skip to content

Instantly share code, notes, and snippets.

@mdolinin
Created November 29, 2018 22:16
Show Gist options
  • Select an option

  • Save mdolinin/045212ceea82ec726b7efcde2169253c to your computer and use it in GitHub Desktop.

Select an option

Save mdolinin/045212ceea82ec726b7efcde2169253c to your computer and use it in GitHub Desktop.
Logging Allure Steps using Spring AOP
package com.example.test.sf.config;
import io.qameta.allure.Step;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import static io.qameta.allure.util.AspectUtils.getParametersMap;
import static io.qameta.allure.util.NamingUtils.processNameTemplate;
@Aspect
public class LoggingStepsAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingStepsAspect.class);
@Around("@annotation(io.qameta.allure.Step) && execution(* *(..))")
public Object step(final ProceedingJoinPoint joinPoint) throws Throwable {
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
final Step step = methodSignature.getMethod().getAnnotation(Step.class);
final String name = Optional.of(step.value())
.filter(StringUtils::isNoneEmpty)
.map(value -> processNameTemplate(value, getParametersMap(methodSignature,
joinPoint.getArgs())))
.orElse(methodSignature.getName());
LOGGER.info("Start step - {}", name);
return joinPoint.proceed();
}
}
package com.example.test.sf.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
public class SpringTestConfig {
@Bean
public LoggingStepsAspect loggingStepsAspect() {
return new LoggingStepsAspect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment