Injecting Spring Boot component with Mockito mocks

Injecting Spring Boot component with Mockito mocks

Not sure if this is a Spring Boot question or a Mockito question.

Not sure if this is a Spring Boot question or a Mockito question.

I have the following Spring Boot @Component class:

@Component
class StartupListener implements ApplicationListener<ContextRefreshedEvent>, KernelConstants {
    @Autowired
    private Fizz fizz;

@Autowired
private Buzz buzz;

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    // Do stuff involving 'fizz' and 'buzz'
}

}

So StartupListener has no constructor and is intentionally a Spring @Component that gets its properties injected via @Autowired.

The @Configuration class providing these dependencies is here, for good measure:

@Configuration
public class MyAppConfiguration {
    @Bean
    public Fizz fizz() {
        return new Fizz("OF COURSE");
    }

@Bean
public Buzz buzz() {
    return new Buzz(1, true, Foo.Bar);
}

}

I am now trying to write a JUnit unit test for StartupListener, and I have been using Mockito with great success. I would like to create a mock Fizz and Buzz instance and inject StartupListenerwith them, but I'm not sure how:

public class StartupListenerTest {
  private StartupListener startupListener;

@Mock private Fizz fizz;

@Mock price Buzz buzz;

@Test public void on_startup_should_do_something() { Mockito.when(fizz.calculateSomething()).thenReturn(43);

// Doesn't matter what I'm testing here, the point is I'd like 'fizz' and 'buzz' to be mockable mocks
// WITHOUT having to add setter methods to StartupListener and calling them from inside test code!

} }

Any ideas as to how I can accomplish this?

javascript testing spring

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Free IAS Mock Test - UPSC Prelims 2020 General Studies Paper

For UPSC Prelims 2020 aspirant, this Mock Test from Free UPSC Mock test Series will help you assess yourself to strengthen your weak Section & learn time management

JavaScript Testing - Unit Tests, Integration Tests & e2e Tests

JavaScript testing - i.e. unit tests, integration tests and e2e (UI) tests - can be intimidating. It shouldn't be! This video guides you through all the basics (including the "Why"?) of JavaScript testing. JavaScript Testing Introduction Tutorial - Unit Tests, Integration Tests & e2e Tests. Master JavaScript testing now!

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview