0

I have a class with methods that has annotation (io.qase.api.annotation.Step)

Class myStepsClass() {

@Step
fun stepOne() {***}

@Step
fun stepTwo() {***}
}

I would like to print "step" method name during the execution, and show a Class name where step method is implemented: in this example this is myStepsClass.

I created Aspect file

@Aspect
class Aspects {

    @Before("@annotation(io.qase.api.annotation.Step)")
    fun stepMethod(joinPoint: JoinPoint) {
        println("Step called: ${getMethodName(joinPoint)}")
    }

    private fun getMethodName(joinPoint: JoinPoint): String {
        val methodSignature = joinPoint.signature as MethodSignature
        return methodSignature.name
    }
}

It prints Step called: stepOne when I call step "stepOne" method in other methods (like Test methods). How to get the parent class name - myStepsClass?

To print something like

Step Called: myStepsClass -> stepOne

Step Called: myStepsClass -> stepTwo

I have created a project with code: https://github.com/heavy-razzer/AssertJ-Maven-Kotlin

mikamika
  • 110
  • 1
  • 6

1 Answers1

0

(joinPoint.signature as MethodSignature).declaringTypeName will do the trick. It will return the full path to the method, split by "." symbol.

So you can have

private fun getParentClassName(joinPoint: JoinPoint): String {
  return (joinPoint.signature as MethodSignature).declaringTypeName.split(".").last()
}

to get name of class, where this method is decribed.

I have updated my GH repo with the example.

Output will be:

Test started: Very simple test
Steps->printCaption(): []
Step: This this a simple test
Test step performed: printCaption
Steps->printMessage(): []
Step: Testing in progress 
Test step performed: printMessage
mikamika
  • 110
  • 1
  • 6
  • Of course, that works. But why make a simple matter complicated and not simply print the whole joinpoint? It is much better for understanding what is going on, because it contains the full method signature including parameter and return types. Why expensively concatenate a subset of this information manually and omit helpful information which is there by default? – kriegaex Sep 03 '22 at 11:12
  • In general, you are right. I did that for two reasons. 1) Just as an example of using different data from JointPoint. 2) in other project I need to display test steps info. And there I don't need full path to the class where that step is defined: its name is enough. Also, I have to hide the password from the method parameters value. In other words, in some cases, I have to remove some unnecessary info from the whole jointpoint to make the log output a bit cleaner and secure. – mikamika Sep 06 '22 at 11:20