What is MapStruct?According to MapStruct website:MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain metho…
MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain method invocations and thus is fast, type-safe and easy to understand.
Inject MapStruct mapper in Blueprint OSGi
Such mappings are sometimes necessary in our integration projects. We also use OSGi to create our applications and Blueprint for dependency injection. Blueprin Maven Plugin makes it very easy to use, providing annotation support.
MapStruct supports component models like cdi, spring and jsr330, so generated classes could be used as beans. Fortunately, Blueprint Maven Plugin uses annotations from JSR 330, such as Singleton or Named.
The only thing we have to do is to add property componentModel with value jsr330 to a mapping interface:
@Mapper(componentModel = "jsr330")
public interface PersonMapper {
Person toDomain(PersonDto personDto);
}
and now we can inject PersonMapper to our beans:
@Singleton
@AllArgsConstructor
public class CreatePersonHandler {
private final PersonRepository personRepository;
private final PersonMapper personMapper;
// ...
}
Blueprint Maven Plugin will generate an XML file with bean PersonMapperImpl and inject it to CreatePersonHandler:
Generate all mappers with JSR 330 annotations
If you have multiple mappers and all of them should be beans, then you can simply add one compiler argument in configuration and all the mappers will have @Singleton and @Named annotations by default.
Few months ago I've came across Groovy - powerful language for JVM platform which combines the power of Java with abilities typical for scripting languages (dynamic typing, metaprogramming).
Together with Groovy I've discovered spock framework (https://code.google.com/p/spock/) - specification framework for Groovy (of course you can test Java classes too!). But spock is not only test/specification framework - it also contains powerful mocking tools.
Even though spock is dedicated for Groovy there is no problem with using it for Java classes tests. In this post I'm going to describe how to configure Maven project to build and run spock specifications together with traditional JUnit tests.
Firstly, we need to prepare pom.xml and add necessary dependencies and plugins.
And that's all for dependencies section. Now we will focus on plugins necessary to compile Groovy classes. We need to add gmaven-plugin with gmaven-runtime-2.0 dependency in plugins section:
With these configuration we can use spock and write our first specifications. But there is one issue: default settings for maven-surefire plugin demand that test classes must end with "..Test" postfix, which is ok when we want to use such naming scheme for our spock tests. But if we want to name them like CommentSpec.groovy or whatever with "..Spec" ending (what in my opinion is much more readable) we need to make little change in surefire plugin configuration:
As you can see there is a little trick ;) We add include directive for standard Java JUnit test ending with "..Test" postfix, but there is also an entry for spock test ending with "..Spec". And there is a trick: we must write "**/*Spec.java", not "**/*Spec.groovy", otherwise Maven will not run spock tests (which is strange and I've spent some time to figure out why Maven can't run my specs). Little update: instead of "*.java" postfix for both types of tests we can write "*.class" what is in my opinion more readable and clean:
With such configuration, we can write either traditional JUnit test and put them in src/test/java directory or groovy spock specifications and place them in src/test/groovy. And both will work together just fine :) In one of my next posts I'll write something about using spock and its mocking abilities in practice, so stay in tune.
Zapraszamy na XI Targi Pracy i Praktyk dla Elektroników i Informatyków.
Odwiedź nasze stoisko w dniach 4-5 marca w godz. 9:30-15:30.
Politechnika Warszawska
Pierwsze piętro budynku Wydziału Elektroniki i Techniki Informacyjnych
Nowowiejska 15/19We invite you to 11th Job and Internship Fair for Electronic Engineers and IT Specialists.
Come and visit our stand between 4-5 March 9:30 am and 15 :30 pm
Warsaw University of Technology
the first floor of the Electronics faculty building
Nowowiejska 15/19