5

I am not good at English. Please understand.

I knew. The use of @SpringBootTest() Annotation to load the entire Bean was clearly thought of as an "Integration Test."

However, the use of @SpringBootTest(classes=mybean.class) was thought to be "Unit Test" because it only runs a certain (selected) "Bean(Class)" although it runs a spring.

I also thought the way to use @ContextConfiguration" was also "Unit Tests." Because I thought only certain classes could be executed, such as @ContextConfusion(classes=myBean.class).

But it was my mistake. I received an answer to the last StackOverflow.

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

My last question--> Whether using @ContextConfiguration can be a "Unit Test" $$$answer ---> 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...

So I ask again.

Is "@WebMvcTest" also "Integration Tests"? I thought "@WebMvcTest" was the "Unit Test." However, based on the last answer, @WebMvcTest also has spring running. So the logic of the last answer is that "@WebMvcTest" is also "Integration Test," but I still don't understand.

I'm confused. Am I just playing with words?

  1. Unit Test vs Integration Test...
  2. Is the @WebMvcTest also an "Integration Test"?
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
sieunkim
  • 381
  • 1
  • 3
  • 6

2 Answers2

8

Quote from Spring doc:

True unit tests typically run extremely quickly, as there is no runtime infrastructure to set up. Emphasizing true unit tests as part of your development methodology can boost your productivity.

Spring is also a kind of runtime infrastructure .So unit test should run without requiring to involve other frameworks even Spring.


Then it has the whole section to talk about Integration testing by introducing Spring TestContext Framework which it describes as :

The Spring Framework provides first-class support for integration testing in the spring-test module. The name of the actual JAR file might include the release version and might also be in the long org.springframework.test form, depending on where you get it from (see the section on Dependency Management for an explanation). This library includes the org.springframework.test package, which contains valuable classes for integration testing with a Spring container.

@ContextConfiguration is come from the Spring TestContext Framework while Spring Boot builds on top on it to provide @SpringBootTest and @WebMvcTest etc. So all these stuff are for Integation testing.

So to summarise, if a test requires starting up Spring in order to run such as @WebMvcTest , it is not a unit test but an integration test.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
3

Firstly, some definitions taken from the Software testing fundamentals web site:

UNIT TESTING is a level of software testing where individual units/ components of a software are tested. The purpose is to validate that each unit of the software performs as designed.

INTEGRATION TESTING is a level of software testing where individual units are combined and tested as a group. The purpose of this level of testing is to expose faults in the interaction between integrated units.

To answer your question, the three annotations @SpringBootTest, @ContextConfiguration, and @WebMvcTest are all involved in loading a subset of Spring components. They are in consequence related to integration testing.

Community
  • 1
  • 1
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240