Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct mapper not injected in Spring Unit Tests

I'm using one mapper generated with MapStruct:

@Mapper
public interface CustomerMapper {
   Customer mapBankCustomerToCustomer(BankCustomerData bankCustomer);
}

The default component model is spring (set in pom.xml)

<compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>

I have a service in which I inject the customer mapper and works fine when I run the application

@Autowired
private CustomerMapper customerMapper;

But when I run unit tests that involves @SpringBootTest

@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class SomeControllerTest {

    @Mock
    private SomeDependency someDependency;

    @InjectMocks
    private SomeController someController;

    @Test
    public void shouldDoSomething() {
        ...
    }

}

I get an org.springframework.beans.factory.UnsatisfiedDependencyException

Unsatisfied dependency expressed through field 'customerMapper'

like image 778
johncol Avatar asked Nov 07 '22 21:11

johncol


1 Answers

I followed this answer and my problem was solved as quickly as I pasted proposed lines in my build.gradle file

like image 68
dwilda Avatar answered Nov 15 '22 05:11

dwilda