Reputation: 1030
I'd like to test my Spring Boot command line application. I would like to mock certain beans (which I was able to do by annotating @ContextConfiguration(classes = TestConfig.class)
at the top of my test class. In TestConfig.class
, I override the beans that I would like to mock. I'd like Spring Boot
to find the rest of the components. This seems to work.
The problem is that when I run the test, the entire application starts up as normal (ie. the run()
method is called).
@Component
public class MyRunner implements CommandLineRunner {
//fields
@Autowired
public MyRunner(Bean1 bean1, Bean2 bean2) {
// constructor code
}
@Override
public void run(String... args) throws Exception {
// run method implementation
}
I've tried to override the MyRunner
@Bean
and put it in TestConfig.class
, but that doesn't seem to work. I understand that I'm loading the regular application context, but that's what I'd like to do (I think?) since I would like to re-use all (or most) of the @Component
I created in my Application, and only mock a tiny subset.
Any suggestions?
EDIT:
Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Upvotes: 5
Views: 7061
Reputation: 42431
CommandLineRunners
are ordinary beans with one exception:
After the application context is loaded, spring boot finds among all its beans the beans that implement this interface and calls their run
method automatically.
Now, I would like you to ask to do the following:
ContextConfiguration
from the test and place a breakpoint in constructor of MyRunner
. The test should look like this:@RunWith(SpringRunner.class) // if you're on junit 4, adjust for junit 5 if you need
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MyTest {
@Autowired
private MyRunner myRunner;
@Test
public void testMe() {
System.out.println("hello");
}
}
run
method is called@RunWith(SpringRunner.class) // if you're on junit 4, adjust for junit 5 if you need
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MyTest {
@MockBean
private MyRunner myRunner;
@Test
public void testMe() {
System.out.println("hello");
}
}
Run the test. Make sure that run
method is not running. Your Application Context now should contain a mock implementation of your component.
If the above works, then the problem is with TestConfig
and ContextConfiguration
annotation. In general when you run without ContextConfiguration
you give spring boot test engine a freedom to mimic the application context started as if its a real application (with autoconfigurations, property resolution, recursive bean scanning and so forth).
However if you put ContextConfiguration, spring boot test doesn't work like this - instead it only loads the beans that you've specified in that configuration. No Autoconfigurations, no recursive bean scanning happens for example.
Update
Based on OP's comment:
It looks like the MyRunner
gets loaded when you put @ContextConfiguration because of component scanning. Since you have an annotation @Component
placed on MyRunner
class it can be discovered by Spring boot engine.
In fact there is a "dangerous" mix of two types of beans definitions here:
1. The beans defined with @Bean
in @Configuration
annotation
2. The beans found during component scanning.
Here is the question for you: If you don't want to mimic the application startup process and instead prefer to load only specific beans, why do you use @SpringBootTest
at all? Maybe you can achieve the goal with:
@RunWith(SpringRunner.class)
@ContextConfiguration(YourConfig.class)
public class MyTest {
...
}
Upvotes: 1
Reputation: 1030
The answer was simpler than I thought. Add the MockBean in
@TestConfiguration
public class TestConfig {
@MockBean
private MyRunner myRunner;
}
We can use the @MockBean to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context.
So MyRunner.run()
is never called but I can still use all the other beans in my application.
Upvotes: 2
Reputation: 7067
One way you could do this is to have 2 classes with the main method, one which sets up the "normal" context, and another that sets up the "mock" context:
Normal App Context, uses the usual Application
@SpringBootApplication(scanBasePackages = "com.example.demo.api")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Foo foo() {
return new Foo("I am not mocked");
}
@Bean
public Bar bar() {
return new Bar("this is never mocked");
}
}
Add another Application
class that overrides the normal context with the mocked one
@SpringBootApplication(scanBasePackageClasses = {MockApplication.class, Application.class})
@Component
public class MockApplication {
public static void main(String[] args) {
SpringApplication.run(MockApplication.class, args);
}
@Bean
public Foo foo() {
return new Foo("I am mocked");
}
}
When you run Application.main
Foo will be "I am not mocked", when you run MockApplication.main()
it will be "I am mocked"
Upvotes: 0