How to write a Junit test for a mapthruct abstract mapper injected via Spring

I am using MapStruct, mapstruct-jdk8 version 1.1.0.Final and define an abstract class that I inject through Spring.

I'm looking at how to test them with a Junit test? I am a basic master cartographer who will use 2 submasters

@Mapper(componentModel = "spring", uses = {SubMapper1.class, SubMapper2.class})
public abstract class MainMapper {

  @Mapping(target = "field1", qualifiedByName = {"MyMapper2Name", "toEntity"})
  public abstract MyEntity toEntity(MyDto pDto);

  public MyDto fromEntity(MyEntity pEntity) {
     // Specific code, hence why I use Abstract class instead of interface. 
  }
}

      

I've tried a few things but can't get the cartographer to be sure to check it out.

@RunWith(SpringRunner.class)
public class MainMapperTest {

    private MainMapper service = Mappers.getMapper(MainMapper.class);

    @Test
    public void testToEntity() throws Exception {
.....

      

java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.mappers.MainMapper

I've also tried via @InjectMock but not diced either.

Unable to create @InjectMocks field named "service". You didn't provide an instance in your field declaration, so I tried to build an instance. However, I failed because: the MainMapper type is an abstract class.

And via Spring @Autowired

Called from: org.springframework.beans.factory.NoSuchBeanDefinitionException: No ready-made component of type "com.mappers.MainMapper": At least 1 component expected to qualify for self-timer. Dependency annotations: {@ org.springframework.beans.factory.annotation.Autowired (required = true)}

I am guessing it might have something to do with the annotation processor and the mapper is not generated when the test is run. I found this class as an example .

However, the AnnotationProcessorTestRunner class doesn't seem to be available until version 1.2, which doesn't have a final version yet.

So my question is how to write Junit tests to test my mapstruct abstract class which I am using with Spring injection in my code.

+7


source to share


3 answers


In response to @Richard Lewan's comment, this is how I declared my test class for the abstract ConfigurationMapper class using 2 subMappers

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ConfigurationMapperImpl.class, SubMapper1Impl.class, SubMapper2Impl.class})
public class ConfigurationMapperTest {

      

You are using the impl generated in annotation SpringBootTest

And then enter the class you want to test



@Autowired
private ConfigurationMapper configurationMapper;

      

Let me know if you need more information, but from there it's easy. I didn't make fun of subMapper as I was better off checking the whole mapping process at once.

+7


source


You have several problems:

  • You should Mappers#getMapper(Class)

    only use the default componentModel

    , otherwise the mapper will not be created correctly. If you get it RuntimeException

    , it means that the implementation class was not generated. Make sure you have the correct setup.
  • You need to test the implementation MainMapperImpl

    , not the abstract class.
  • If you want to test with spring bean then you need to use the correct one ComponentScan

    and make sure the implementation and used mappers can be auto-updated.


The class you linked is the wrong test class and is not linked to your test case. Have a look at this integration test for spring integration.

AnnotationProcessorTestRunner

is part of our tests and is used to test the annotation processor and has been around since the beginning. This is not part of the releases.

+1


source


Compounding this:

  • MainMapper

    introduced in @Component ConverterUsingMainMapper

You can use the following example:

@RunWith(SpringRunner.class)
@ContextConfiguration
public class ConsentConverterTest {

    @Autowired
    MainMapper MainMapper;

    @Autowired
    ConverterUsingMainMapper converter;

    @Configuration
    public static class Config {

        @Bean
        public ConverterUsingMainMapper converterUsingMainMapper() {
            return new ConverterUsingMainMapper();
        }

        @Bean
        public MainMapper mainMapper() {
            return Mappers.getMapper(MainMapper.class);
        }
    }


    @Test
    public void test1() {
        // ... your test.
    }

}

      

0


source







All Articles