title: Test Unit Spring boot: Unable to register mock bean
body: <p>I have two classes of tests: 1- for the unit test of the Controller class and 2- for the unit test of the Service class as below:</p>
  
  <p><strong>1- Test class controller:</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  @AutoConfigureMockMvc
  public class CentroDeCustoRestControllerTeste {
  
      @Autowired
      private MockMvc mvc;
  
      @MockBean
      private CentroDeCustoService service;
  
      @Test
      @WithMockUser(username = "teste-app@teste.com", authorities = {"ADMIN"})
      public void aoBuscarUmCentroDeCustoRetornarUmJsonComPropriedadeCentroDeCusto() throws Exception {
          CentroDeCusto centroDeCusto = new CentroDeCusto();
          centroDeCusto.setNome("Financeiro");
  
          given(service.centroDeCusto(1L)).willReturn(Optional.of(centroDeCusto));
  
          mvc.perform(get("/centrosdecustos/1").contentType(MediaType.APPLICATION_JSON))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath("$.centroDeCusto", org.hamcrest.core.Is.is(centroDeCusto.getCentroDeCusto())));
          }
       }
  </code></pre>
  
  <p><strong>2- Test class Service:</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class CentroDeCustoServiceImplTeste {
  
      @TestConfiguration
      static class CentroDeCustoServiceImplConfiguracaoContexto {
          @Bean
          public CentroDeCustoService centroDeCustoService() {
              return new CentroDeCustoServiceImpl();
          }
      }
  
      @Autowired
      private CentroDeCustoService service;
  
      @MockBean
      private CentroDeCustoRepository repository;
  
      @Before
      public void setup() {
          CentroDeCusto centroDeCusto = new CentroDeCusto();
          centroDeCusto.setId(1L);
          Optional&lt;CentroDeCusto&gt; centroDeCustoOpt = Optional.of(centroDeCusto);
  
          Mockito.when(repository.findById(1L)).thenReturn(centroDeCustoOpt);
      }
  
      @Test
      public void aoBuscarUmCentroDeCustoExistenteRetornarUmCentroDeCustoOptional() {
          Optional&lt;CentroDeCusto&gt; resultado = service.centroDeCusto(1L);
          CentroDeCusto centroDeCusto = resultado.get();
  
          Assertions.assertThat(centroDeCusto.getId())
                  .isEqualTo(1L);
      }
  }
  </code></pre>
  
  <p><strong>Interface CentroDeCustoService</strong> </p>
  
  <pre><code>public interface CentroDeCustoService {
  
      Optional&lt;CentroDeCusto&gt; centroDeCusto(Long id);
  
      Page&lt;CentroDeCusto&gt; centrosDeCusto(Pageable pagina);
  
      Optional&lt;CentroDeCusto&gt; buscaCentroDeCustoPeloNumero(String numero);
  
      List&lt;CentroDeCusto&gt; buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial);
  
      boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto);
  
      CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto);
  
      CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto);
  
      boolean delataCentroDeCusto(Long id);
  }
  </code></pre>
  
  <p><strong>Class CentroDeCustoServiceImpl</strong> </p>
  
  <pre><code>@Service
  public class CentroDeCustoServiceImpl implements CentroDeCustoService {
      @Autowired
      private CentroDeCustoRepository repository;
  
      @Override
      public Optional&lt;CentroDeCusto&gt; centroDeCusto(Long id) {
          return repository.findById(id);
      }
  
      @Override
      public Page&lt;CentroDeCusto&gt; centrosDeCusto(Pageable pagina) {
          return repository.departamentosPorDataDeAtualizacao(pagina);
      }
  
      @Override
      public Optional&lt;CentroDeCusto&gt; buscaCentroDeCustoPeloNumero(String numero) {
          return repository.findByCentroDeCusto(numero);
      }
  
      @Override
      public List&lt;CentroDeCusto&gt; buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial) {
          return repository.findByContasGerenciaisId(idDaContaGerencial);
      }
  
      @Override
      public boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto) {
          String numeroDoCentroDeCusto = centroDeCusto.getCentroDeCusto();
          Long idDoCentroDeCusto = centroDeCusto.getId();
          Optional&lt;CentroDeCusto&gt; centroDeCustoOpt = repository.findByCentroDeCustoAndIdNotIn(numeroDoCentroDeCusto, idDoCentroDeCusto);
  
          if (centroDeCustoOpt.isPresent())
              return true;
  
          return false;
      }
  
      @Override
      public CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto) {
          return repository.save(centroDeCusto);
      }
  
      @Override
      public CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto) {
          return repository.save(centroDeCusto);
      }
  
      @Override
      public boolean delataCentroDeCusto(Long id) {
          Optional&lt;CentroDeCusto&gt; centroDeCustoOpt = centroDeCusto(id);
          if (centroDeCustoOpt.isPresent()) {
              repository.delete(centroDeCustoOpt.get());
              return true;
          }
  
          return false;
      }
  }
  </code></pre>
  
  <p>When I run the controller it seems that the test created for the Service class is impacting because both tests call the CentroDeCustoService object. How can I correctly configure test classes so that there are no more conflicts?</p>
  
  <p>Error: <strong>Unable to register mock bean br.com.teste.reembolso.centrocusto.CentroDeCustoService expected a single matching bean to replace but found [centroDeCustoService, centroDeCustoServiceImpl]</strong>:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
      at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
      at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
      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$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      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.run(ParentRunner.java:363)
      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:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: java.lang.IllegalStateException: Unable to register mock bean br.com.teste.reembolso.centrocusto.CentroDeCustoService expected a single matching bean to replace but found [centroDeCustoService, centroDeCustoServiceImpl]
      at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.getBeanName(MockitoPostProcessor.java:239)
      at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.registerMock(MockitoPostProcessor.java:184)
      at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.register(MockitoPostProcessor.java:174)
      at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:144)
      at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:131)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:282)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:170)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
      at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
      ... 24 more
  </code></pre>
  
  <p>It looks like it's conflicting but I do not know how to solve it. can anybody help me?</p>