Reputation: 326
I have a project (https://github.com/serjteplov/demo-kafka-mock.git) where there are couple of dummy functions to read messages from kafka. In one of these functions crudRepository is used to perform some operations against DB:
@EnableAutoConfiguration
public class SampleFunctionConfiguration {
@Bean
public Function<String, String> uppercase(UserRepository userRepository) {
String id = userRepository.findById("abc").map(User::getId).orElse(null);
return value -> value.toUpperCase() + " id=" + id;
}
@Bean
public Function<String, String> reverse() {
return value -> new StringBuilder(value).reverse().toString();
}
}
So the problem is to write test on uppercase binder function. To make this test works correctly I have to mock such call
userRepository.findById("abc")
then I've created mock and added it to the context:
@Primary
@Bean("test")
UserRepository userRepository() {
return new DetachedMockFactory().Mock(UserRepository)
}
and mock call in the test:
userRepository.findById("abc") >> Optional.of(new User("abc", "Bob"))
After test execution mock is successfully created, but userRepository.findById("abc") still returns null.
Can anyone tell how to fix this problem? Or any alternative implementations and workarounds would be nice
Upvotes: 0
Views: 164
Reputation: 174554
You are invoking the stubbed method in the bean definition instead of in the function.
@Bean
public Function<String, String> uppercase(UserRepository userRepository) {
String id = userRepository.findById("abc").map(User::getId).orElse(null);
return value -> value.toUpperCase() + " id=" + id;
}
Vs.
@Bean
public Function<String, String> uppercase(UserRepository userRepository) {
return value -> {
String id = userRepository.findById("abc").map(User::getId).orElse(null);
return value.toUpperCase() + " id=" + id;
};
}
Upvotes: 1