How can I find out if code is running inside a JUnit test or not?

前端 未结 11 1278
梦毁少年i
梦毁少年i 2020-12-24 10:34

In my code I need to do certain fixes only when it is run inside a JUnit test. How can I find out if code is running inside a JUnit test or not? Is there something like JUni

相关标签:
11条回答
  • 2020-12-24 10:54

    In order to distinguish between test and no test you can always define special property or value in application-test.properties.

    0 讨论(0)
  • 2020-12-24 10:58

    Lots of people on this thread say that it's a bad idea for code to run slightly differently while under JUnit. I generally agree, but I think there are some exceptions.

    For example, I am currently writing INTEGRATION (as opposed to Unit) tests for an app that connects to a DB.

    These acceptance tests often need to completely reinitialize the DB with specific test data.

    Obviously, I don't want this EVER, EVER to be done on an actual production DB, because that might completely erase valuable production data.

    The easiest way to guarantee that this will never happen, is to make it impossible for the code to connect to a production DB when it is running under JUnit. This in turn can be done if, for example, the Factory that generates a connection can tell that it's running under JUnit, and in that case, will return a null connection unless the database we are trying to connect to has a name that is known to be a test database (ex: "testdatabase").

    0 讨论(0)
  • This has worked for me:

    private Boolean isRunningTest = null;
    
    private boolean isRunningTest() {
        if (isRunningTest == null) {
            isRunningTest = true;
            try {
                Class.forName("org.junit.Test");
            } catch (ClassNotFoundException e) {
                isRunningTest = false;
            }
        }
        return isRunningTest;
    }
    

    It can be used in Maven tests, as well as inside the Eclipse IDE, as long as junit is included as a dependency with scope "test", eg:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    
    0 讨论(0)
  • 2020-12-24 11:01

    I can find a justification for this, which is when you want to provide code in a production class to help testing. This would be similar to the Java assert, which only applies when the debug flag is set.

    Something like this:

    Object debugState() {
        // This is only meant to be called when testing
        if (!JUnit.isRunning()) {
            throw new IllegalStateException("Not in a test!");
        }
        // Now compute possibly costly debug information
        // not to be used in production
        Object state = ...
    }
    
    0 讨论(0)
  • 2020-12-24 11:05

    First of all, this a probably not a good idea. You should be unit testing the actual production code, not slightly different code.

    If you really want to do this, you could look at the stacktrace, but since you are changing your program for this anyway, you might just as well introduce a new static boolean field isUnitTesting in your code, and have JUnit set this to true. Keep it simple.

    0 讨论(0)
提交回复
热议问题