@SpringBootTest vs @ContextConfiguration vs @Import in Spring Boot Unit Test

I think all 3 presented options are bad if your intent is to run a proper unit test.
A unit test must be blazing fast, you should be able to run hundreds of those in a second or so (depending on the hardware of course, but you get the idea).
So once you say “I start spring for each test” – it’s not a unit test anymore.
Starting spring for each test is a very expensive operation.

What’s interesting is that your code of CoffeeService is written in a way that it’s perfectly testable: Just use some library like Mockito to mock the repository class and you can test the service logic without any spring at all.
You won’t need any spring runner, any spring annotations. You’ll also see that these tests are running much faster.

class MyServiceTest {

    public void test_my_service_get_coffee_logic() {
           // setup:
           CoffeeRepository repo = Mockito.mock(CoffeeRepository.class);

           CoffeeService underTest = new CoffeeService(repo);

           // when:
           String actualCoffee  =  underTest.getCoffee("mocha");

           // then:
           assertEquals(actualCoffee, "coffeeFound");

Now regarding spring test library

You can think about it as a way to test code that requires some interconnections with other components and its problematic to mock everything out. Its a kind of integration test inside the same JVM.
All the ways that you’ve presented run an Application Context and this is a very complicated thing actually under the hood, there are entire sessions on youtube about what really happens during the application context startup – although, beyond the scope of the question, the point is that it takes time to execute the context startup

@SpringBootTest goes further and tries to mimic the processes added by Spring Boot framework for creating the context: Decides what to scan based on package structures, loads external configurations from predefined locations optionally runs autoconfiguration starters and so on and so forth.

Now the application context that might load all the beans in the application can be very big, and for some tests, it’s not required.
Its usually depends on what is the purpose of the test

For Example, if you test rest controllers (that you’ve placed all the annotations correctly) probably you don’t need to start up DB connections.

All the ways you’ve presented filter what exactly should be run, what beans to load and to inject into each other.

Usually, these restrictions are applied to “layers” and not to single beans (layers = rest layer, data layer and so forth).

The second and third methods are actually the same, they are different ways to “filter” the application context preserving only the necessary beans.


Since you’ve already done the performance comparison of the methods:

Unit test = very fast test, it’s purpose is to verify the code you’ve written (or one of your colleagues of course)
So if you run Spring its automatically means a relatively slow test. So to answer your question

Whether using @ContextConfiguration can be a “Unit Test”

No, it cannot, it’s an integration test that runs only one class in spring.

Usually, we don’t run only one class with Spring Framework. What is the benefit of running it inside the spring container if you only want to test the code of one class (a unit)? Yes, in some cases it can be a couple of classes, but not tens or hundreds.

If you run one class with spring then, in any case, you’ll have to mock all its dependencies, the same can be done with mockito…

Now regarding your questions

@ContextConfiguration vs. @SpringBootTest technical differences.

@SpringBootTest is relevant only if you have a Spring Boot application. This framework uses Spring under the hood but, in a nutshell, comes with many pre-defined recipes/practices of how to write the “infrastructure” of the application:

  • configuration management,
  • package structure,
  • pluggability
  • logging
  • database integration etc.

So Spring Boot establishes well-defined processes to deal with all the aforementioned items, and if you want to start the test that will mimic the spring boot application, then you use @SpringBootTest annotation. Otherwise (or in case you have only spring driven application and not a spring boot) – don’t use it at all.

@ContextConfiguration is an entirely different thing though. It just says what beans would you like to use in Spring driven application (it also works with spring boot)

Is “Unit Test” the correct way to use @ContextConfiguration? Or not?

As I said – all the spring test related stuff is for integration testing only, so no, it’s a wrong way to be used in unit tests. For unit tests go with something that doesn’t use spring at all (like mockito for mocks and a regular junit test without spring runner).

Leave a Comment