Answer the question
In order to leave comments, you need to log in
Why doesn't autowired work on this object?
For example, here is my class for testing (we are testing a standard controller with a repository. Class names are random - this is just an example).
Above the class, I do not specify anything, except for @ExtendWith(MockitoExtension.class), as I understand it, spring boot will figure it out for itself that this is a test.
What I'm most interested in is why does employeeService autowire, but clock doesn't and throws a null pointer? And how to make it so that I can autowire this bean.
@ExtendWith(MockitoExtension.class)
class EmployeeServiceTest {
@Mock
ExampleRepository exampleRepository;
@Autowired
Clock clock;
@Autowired
@InjectMocks
EmployeeService employeeService;
// methods omitted
}
@Configuration
public class TimeConfig {
@Bean
public Clock clock() {
return Clock.systemDefaultZone();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question