-1

I would like to deactivate some functions at the runtime when the application is running in a Unit Test mode. Is there a way to recognize it?

Thanks for any tips!

vaio
  • 91
  • 11
  • Can you explain more about what you want to deactivate? – Todd Jun 10 '17 at 15:14
  • Need more details. Please provide examples – Rohan Rayarikar Jun 10 '17 at 15:15
  • I have a Spring service that creates a user and sends an email after the creation. When I am running the JUnit-Tests this service is called more than 10 times, so that I receive 10 e-mails. I would like to deactivate the sending of e-mails when running Unit-Tests. I'm thinking of something like if (Junit != active) sendEmail(...). – vaio Jun 10 '17 at 15:17
  • 2
    If the application is running, then you're not doing unit tests. Unit tests test an isolated unit of code, not an application. Unit tests use mocks to isolate the code under test from it's dependencies – JB Nizet Jun 10 '17 at 15:17
  • 1
    That is not what testing is for. If your application does something different in production then in testing then what you writing tests for? – luk2302 Jun 10 '17 at 15:18
  • That is what injection is for, you have to inject a mock which does not send the email. – luk2302 Jun 10 '17 at 15:18
  • Yup, I was going to mention dependency injection too. First, units of code should be designed so that they don't assume the full app is running. Isolate (or encapsulate) their behavior so that they can be tested. Second you must inject their state so that they don't have to know when they are being tested. Ideally execution should be the same for both production and test. In practice it won't always be but you should still strive for that goal. – markspace Jun 10 '17 at 15:24
  • Yes, thanks for all the comments. But I am still wondering if it is theoretically possible to recognize whether the app is running in production or in Unit-Mode. – vaio Jun 10 '17 at 15:27
  • Get stack trace with `Thread.currentThread().getStackTrace()` and find JUnit test runner in it. –  Jun 10 '17 at 15:29
  • Checking stack trace is a hack and shows a poor design and implementation, especially in production code that checks junit runtime. – tsolakp Jun 10 '17 at 16:18

2 Answers2

0

Thanks for tips!

This code works for my purposes:

public static boolean junitIsActive() {     
    for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
        if (s.getClassName().contains("org.junit.runners.model")) {
            return true;
        }
    }       
    return false;
}
vaio
  • 91
  • 11
0

Thanks @vaio.

@Bean
boolean testMode() {
    for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
        if (s.getClassName().contains("org.springframework.boot.test.context.SpringBootContextLoader")) {
            return true;
        }
    }
    return false;
}

In my case (Spring boot batch), above code will work.

I just want to make some Job not run in test mode.

    @Bean
public Job reindexLeadsJob(JobCompletionNotificationListener listener, Step stepReindexFromMongoDB) {
    if (testMode) {
        return null;
    }
    return jobBuilderFactory.get("reindexLeadsJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .flow(stepReindexFromMongoDB)
            .end()
            .build();
}

Now in test mode, I simple return null in the JobConfig. Is there other way more elegant?

ice6
  • 1,173
  • 9
  • 9