springboottest how to prevent running application

1k views Asked by At

I have the standard Application class which runs a Spring batch ETL:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

with my Junit test doing something like:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyServiceTest {

    @Autowired
    MyService myService;

    @Test
    public void testInsertions() {
        //do stuff with assertions
    }
}

My problem is that when I execute the junit test, the application also kicks off the ETL then executes the test. How to prevent the application from running?

1

There are 1 answers

0
bruno.zambiazi On

I think there are a lot of alternatives and it really depends on what you are trying to achieve.

One of the options would be to run your tests with a specific profile, like testing, and configure your ETLs (I'm assuming they are just jobs) to be configured based on some property or specific profile.

For example:

Testing class

@ActiveProfiles("testing")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyServiceTest {
    ...
}

Job/ETL classes

@Component
@Profile("!testing")
public class JobEtlService {
}

Hope it helps.