To answer the question when you want and do not want to use it is part of the question.
When to use SpringJUnit4ClassRunner
IMO SpringJUnit4ClassRunner should be used very sparingly. Running the Spring container to run the unit test involves significant overhead.
I usually use SpringJUnit4ClassRunner to check:
- that the components are inserted (automatically connected), as expected
- this configuration data is entered as expected
At the very least, I always have a simple SpringJUnit4ClassRunner test to test the health of the Spring container. Ok
Component injection can cause problems if the @Qualifier annotation @Qualifier not used or is being used incorrectly. A health check test will detect such problems.
When loading a configuration from multiple yaml files, you may need to verify that the cards are combined, as expected, with the appropriate overrides.
When not to use SpringJUnit4ClassRunner
I would not use SpringJUnit4ClassRunner to test non-Spring functions in the test code. Which in my experience means most of the functionality.
Thus, this means that any automatically connected components and entered configuration data must be checked. This may mean a little setup code for your unit tests. However, this installation code needs to be written only once for all tests in the tested class. It is also much faster to run unit tests with proven components.
I keep mocking and using Spock to make fun of the ingredients. Example winding code:
import spock.lang.Specification class FooManagerTest extends Specification { FooManager cut void createMockFooReporter() { FooReporter mockFooReporter = Mock(FooReporter) mockFooReporter.fooFormatter = Mock(FooFormatter) } void setup() { cut = new FooManager() cut.fooReporter = createMockFooReporter() } void "Basic test"() {
In this example, the FooManager class under test has a FooReporter FooReporter , which itself contains a FooFormatter FooFormatter .
Joman68
source share