Reputation: 165
I am trying to mock a service using @MockBean annotation, but I am getting the following error:
org.springframework.beans.factory.BeanCreationException: Could not inject field: FooService FooTest.fooService; nested exception is java.lang.IllegalStateException: FooService fooService cannot have an existing value
Here is the code sample I try with:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import FooService;
@RunWith (SpringRunner.class)
@SpringBootTest
class FooTest {
@Autowired
FooService fooService;
@MockBean
BarService barService;
@Test
public void testMethod() throws Exception {
Mockito.doThrow(new ExceptionClass()).when(barService).externalWebSerivceCall();
//Verify @Retryable is calling 3 times
}
}
class FooService {
@Autowired
BarService barService;
@Retryable (value = ExceptionClass.class, maxAttempts = 3, backoff = @Backoff (delay = 3 * 60 * 1000))
public void methodThatFails() {
barService.externalWebSerivceCall();
}
}
Any idea how I can resolve it?
Here is the entire stacktrace:
org.springframework.beans.factory.BeanCreationException: Could not inject field: FooService FooTest.fooService; nested exception is java.lang.IllegalStateException: The field FooService FooTest.fooService cannot have an existing value
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.inject(MockitoPostProcessor.java:364)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.inject(MockitoPostProcessor.java:352)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.lambda$injectFields$0(MockitoTestExecutionListener.java:79)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:100)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.IllegalStateException: The field FooService FooTest.fooService cannot have an existing value
at org.springframework.util.Assert.state(Assert.java:97)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.inject(MockitoPostProcessor.java:358)
... 27 more
Upvotes: 3
Views: 16991
Reputation: 930
I noticed that there is no @Service
annotation on your Service class. One reason @MockBean
might not be working is if you do not label the class as @Service
or @Component
.
If that’s not it, I’m not sure exactly why the @MockBean
annotation is not working for you, but depending on what you are trying to test exactly you might want to use just @RunWith(SpringRunner.class)
with an @TestConfiguration
method and @Autowired
for your Service instead of using @SpringBootTest
on your class. This definitely works and would allow you to test the Service layer.
Here's an example that I've tested based on this article which says:
To check the Service class, we need to have an instance of the Service class created and available as a @Bean so that we can @Autowire it in our test class. We can achieve this configuration using the @TestConfiguration annotation.
During component scanning, we might find that components or configurations created only for specific tests accidentally get picked up everywhere. To help prevent this, Spring Boot provides the @TestConfiguration annotation that we can add on classes in src/test/java to indicate that they should not be picked up by scanning.
@RunWith(SpringRunner.class)
@DataJpaTest//good for working with JPA Components
// @SpringBootTest
class Tests {
@TestConfiguration //tell spring that this configuration is only for testing environment
static class UserDetailsServiceImplTextContext {
@Bean //needed for creating instance for test autowiring
public UserDetailsServiceImpl userDetailsServiceImpl() {
return new UserDetailsServiceImpl();
}
}
@Autowired
private UserDetailsServiceImpl userServices;
@Autowired
TestEntityManager entityManager;
@Test
@DisplayName("Context Loads")
void contextLoads() {
}
@Test
@DisplayName("Create User")
void createUser() {
//given
String a = "testUser";
String email = "[email protected]";
String username = a;
String firstname = a;
String lastname = a;
String password = a;
String userType = "USER";
User u = new User(email, username, firstname, lastname, password, userType);
//when
entityManager.persist(u);
entityManager.flush();
//then
User u2 = userServices.getUserRepo().findByUsername(username);
//expect
assertEquals(u, u2);
}
}
Also here's another section of the article on the topic that might be helpful. It's an alternative example using @SpringBootTest showing the additional configuration involved.
Upvotes: 2