title: MapStruct not autowring with Spring-boot testing Gradle Junit5
body: <p>I am trying to use mapstruct with Gradle, but I am having limited success. When I'm using it in the application everything seems to be working fine, but when I tried to write some tests Spring can't autowire MapStruct properly (it just returns NullPointer exceptions). I am using Gradle 5.4.1, Junit5 and IntelliJ 2019.1.2.</p>
  <p>This is the build folder and there is no mapper generated for test classes.</p>
  <p><a href="https://i.stack.imgur.com/v1liq.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Repository with the code is here:
  <a href="https://github.com/MirkoManojlovic/mapstruct-example" rel="nofollow noreferrer">https://github.com/MirkoManojlovic/mapstruct-example</a></p>
  <h3>Mapper:</h3>
  <pre class="lang-java prettyprint-override"><code>@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN,
          componentModel = &quot;spring&quot;,
          injectionStrategy = InjectionStrategy.CONSTRUCTOR)
  public interface ItemMapper {
      ItemDto toDto(Item item);
      Item toItem(ItemDto itemDto);
  }
  
  </code></pre>
  <h3>Repository:</h3>
  <pre class="lang-java prettyprint-override"><code>public class ItemRepository {
      public ItemDto getItemDto() {
          return new ItemDto(&quot;item 1&quot;);
      }
  
      public Item getItem() {
          return new Item(1, &quot;item 1&quot;, 20);
      }
  }
  </code></pre>
  <h3>Service:</h3>
  <pre class="lang-java prettyprint-override"><code>@Service
  @RequiredArgsConstructor
  @Log4j2
  public class ItemService {
      private final ItemRepository itemRepository;
      private final ItemMapper itemMapper;
  
      public ItemDto getItemDto() {
          Item item = itemRepository.getItem();
          ItemDto itemDto = itemMapper.toDto(item);
          log.info(itemDto);
          return itemDto;
      }
  
      public Item getItem() {
          ItemDto itemDto = itemRepository.getItemDto();
          Item item = itemMapper.toItem(itemDto);
          log.info(item);
          return item;
      }
  }
  </code></pre>
  <h3>Test:</h3>
  <pre class="lang-java prettyprint-override"><code>@ExtendWith(MockitoExtension.class)
  public class ItemServiceTest {
  
      @Mock
      private ItemRepository itemRepository;
  
      @InjectMocks
      private ItemService itemService;
  
      @Spy
      private ItemMapper itemMapper;
  
      @Test
      void shouldReturnItemDto() {
          Item mockItem = new Item(1, &quot;mockItem&quot;, 10);
          given(itemRepository.getItem()).willReturn(mockItem);
          ItemDto itemDto = itemService.getItemDto();
          assertThat(mockItem.getName()).isEqualTo(itemDto.getName());
      }
  
  }
  </code></pre>
  <h3>build.gradle:</h3>
  <pre><code>plugins {
      id 'org.springframework.boot' version '2.1.5.RELEASE'
      id 'java'
  }
  
  apply plugin: 'io.spring.dependency-management'
  
  group = 'com.mapstruct.spring.example'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '1.8'
  
  configurations {
      compileOnly {
          extendsFrom annotationProcessor
      }
  }
  
  repositories {
      mavenCentral()
  }
  
  test {
      useJUnitPlatform {
      }
  }
  
  dependencies {
      // Mapstruct
      implementation 'org.mapstruct:mapstruct:1.3.0.Final'
      annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final'
      testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.3.0.Final'
  
      implementation 'org.springframework.boot:spring-boot-starter-web'
      compileOnly 'org.projectlombok:lombok'
      annotationProcessor 'org.projectlombok:lombok'
  
      testImplementation('org.springframework.boot:spring-boot-starter-test')
  
      // JUnit5
      testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2'
      testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
      testImplementation 'org.junit.jupiter:junit-jupiter-params:5.3.2'
  
      // Mockito
      testImplementation 'org.mockito:mockito-core:2.23.4'
      testImplementation 'org.mockito:mockito-junit-jupiter:2.23.4'
  }
  
  </code></pre>