title: Spring Boot - @MockBean Creating Duplicate in Test Case
body: <p>I attempting to &quot;mock&quot; a bean in <code>@SpringBootTest</code>.</p>
  <pre><code>@SpringBootTest
  @ActiveProfiles(&quot;test&quot;)
  @RunWith(SpringRunner.class)
  public class SalesSummaryServiceTest {
  
      @MockBean
      private SalesSummaryMapper summmaryMapper;
      
      @Autowired
      private SalesSummaryService salesSummmaryService;
  
      @Test
      public void testGetMonthlySummary() {
  
          // Removed for brevity
  
          given(this.summmaryMapper.selectByPropDate(testSchema, testProp, testDate, testEnd)).willReturn(List.of(testSum, testSum));     
          var res = this.salesSummmaryService.getMonthlySummary(test, testProp, testDate.getMonthValue(), testDate.getYear());
  
          // Removed for brevity
          
          
          
      }
  
  }
  </code></pre>
  <p>However, the test fails (Application Context fails to load) because no unique bean can be found:</p>
  <pre><code>Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.midamcorp.data.dal.SalesSummaryMapper' available: expected single matching bean but found 2: salesSummaryMapper,com.midamcorp.data.dal.SalesSummaryMapper#0
  </code></pre>
  <p>The class under test is a basic <code>@Service</code> implementation:</p>
  <pre><code>@Service
  public class SalesSummaryServiceImpl implements SalesSummaryService {
  
      private SalesSummaryMapper salesSumMapper;
      
      @Autowired
      public SalesSummaryServiceImpl(SalesSummaryMapper salesSumMapper) {
          this.salesSumMapper = salesSumMapper;
      }
  
      @Override
      public List&lt;SaleSummary&gt; getMonthlySummary(PropType propType, String propId, int monthVal, int yearVal) {
          var startDate = LocalDate.of(yearVal, monthVal, 1);
          var endDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
          return this.salesSumMapper.selectByPropDate(propType.getSchema(), propId, startDate, endDate);
      }
  }
  </code></pre>
  <p>The <code>SaleSummaryMapper</code> dependency is a simple MyBatis mapper:</p>
  <pre><code>import org.apache.ibatis.annotations.Mapper;
  
  @Mapper
  public interface SalesSummaryMapper {
  }
  </code></pre>
  <p>I understand the basic error (e.g. Spring cannot determine which bean to use as two could satisify the dependency). What I do not understand is <em>why</em> the &quot;real&quot; mapper bean is not being replaced by the <code>@MockBean</code> when, per the docs: &quot;Any existing single bean of the same type defined in the context will be replaced by the mock&quot;. What am I missing? Thanks!</p>