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?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

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!

Unit Testing in JavaScript and Jasmine

Learn how to unit test your JavaScript using the jasmine framework, from your first test to continuous integration. Jasmine is one of the popular JavaScript unit testing frameworks which is capable of testing synchronous and asynchronous JavaScript code. Unit Testing in JavaScript and Jasmine. Jasmine Unit Test Tutorial

Jest Crash Course - Unit Testing in JavaScript

In this JavaScript Testing tutorial, we will get started with JavaScript unit testing using Jest. We will look at how to setup Jest, different matchers as well as how to test async code, run functions before and after different tests, etc. This is for developers that are fairly new to testing and the Jest testing framework. Jest Crash Course - Unit Testing in JavaScript