Disabling certain aspects during unit test runs

前端 未结 4 2074
我在风中等你
我在风中等你 2021-01-04 01:21

I have integration tests (load context) and unit tests running together. My code does aspectj compile time weaving using spring.

My problem is that my declared advi

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 01:55

    You can write a method that returns if current execution was launched using JUnit framework.

    The method can check stack trace with Thread.currentThread().getStackTrace() and search for MockitoJUnitRunner presence.

    I tested this solution using a SpringJUnit4ClassRunner but I think could work with MockitoJUnitRunner.

    Also, you can have got a static boolean field like:

    private static boolean TEST_ENVIRONMENT = false;
    

    In a class present in your project (not in your tests) and check the value in the control method instead of use stack trace.

    When you run your tests, you can use @BeforeClass annotation to set TEST_ENVIRONMENT = true.

    This solution only gives you a way to know if your code is running from a test or not.

提交回复
热议问题