0

In my spring boot project, I am using MockMVC to test controller(web) layer. But I also have AOP(AspectJ) logic in my project, when I run unit test for controller with MockMVC, the test also triggers AOP code, how can I prevent AOP code to be triggered while running unit test for controller?

@Test
public void testMyControllerMethod() {
   ...

   // myRequest hits an endpoint function of my controller, there is also AOP intercept the function call, how can I disable AOP to be triggered while running test?
   mockMVC.perform(myRequest).andExpect(okStatus)

}

Question is in my code comment :)

I have checked this answer, I understand to use the if() expression, but I don't get TestMode.ACTIVE, there is no such thing in Spring boot. If someone could let me know how to check whether code is running unit test or not at runtime, I would know how to prevent AOP logic run as well.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Leem
  • 17,220
  • 36
  • 109
  • 159
  • Possible duplicate of [Disabling certain aspects during unit test runs](https://stackoverflow.com/questions/7433658/disabling-certain-aspects-during-unit-test-runs) – Simon Martinelli Aug 08 '19 at 13:35
  • Yes, but the answer doesn't work for me, e.g. how can I know the code is running under test mode? That answer mentioned `TestMode.ACTIVE`, I don't know where that comes from, there is no TestMode class in spring boot. – Leem Aug 08 '19 at 13:38
  • That's a global variable that you set in the tests. The aspects have to check for this variable. It's a manual solution not something that comes from Spring – Simon Martinelli Aug 08 '19 at 13:42
  • Could you please provide a simple skeleton example of that glable variable? Where to put it and where to update the value? – Leem Aug 08 '19 at 13:44
  • public final boolean TestMode.ACTIVE = false; And in the test you set TestMode.ACTIVE = true; – Simon Martinelli Aug 08 '19 at 14:14
  • Thanks, I got another issue related with this answer: https://stackoverflow.com/questions/57418907/pointcut-expression-if-contains-unsupported-pointcut-primitive-if – Leem Aug 08 '19 at 19:04

1 Answers1

0

What I meant in the other answer, as Simon already tried to explain to you, is something like this:

package de.scrum_master.app;

public class TestMode {
  public static boolean ACTIVE = false;
}

But actually there I also listed a few other options such as environment variables and system properties. If I were you I would use one of those because in your Maven or Gradle build it would be very easy to set properties or environment variables via configuration. Your if() pointcut could access those variables.


Especially in the context of Spring there is an even simpler option: a test application configuration. Just provide a configuration without aspects to your tests. That way you can have different configurations for

  • production environment,
  • unit tests (no aspects),
  • integration tests (e.g. with aspects but different from unit test and production).
  • et cetera.

The advantage here is that you don't need any if() pointcuts or build any other knowledge about test/production environments into your aspects, which is quite ugly. My other answer only shows what you can do, it does not say it is the best solution.

kriegaex
  • 63,017
  • 15
  • 111
  • 202