2

I have searched StackOverflow for an answer to my question. While I have found many offered solutions, none work in my situation.

I have to use MapStruct for a project at work, so I am trying to learn it through Tutorialspoint. However, instead of using Maven as the tutorial suggests, I am using Gradle, which I have to do because my company uses Gradle instead of Maven. I have seen answers in StackOverflow that moving the project to Maven gets it to work, but that is not an option in my case.

I am using version 1.4.2.Final instead of 1.5.0.RC1 because I had a different issue when using the latter that resolved by switching to the former.

Here are the parts of my code:

build.gradle

plugins { id 'org.springframework.boot' version '2.6.5' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.tutorialspoint' version = '2.4.2' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.4.2.Final' implementation 'org.mapstruct:mapstruct:1.4.2.Final' annotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final" compileOnly 'org.mapstruct:mapstruct-processor:1.4.2.Final' } tasks.named('test') { useJUnitPlatform() } compileJava { options.compilerArgs += [ '-Amapstruct.suppressGeneratorTimestamp=true', '-Amapstruct.suppressGeneratorVersionInfoComment=true', '-Amapstruct.verbose=true' ] } 

Student.java

package com.tutorialspoint.mapstructproject; public class Student { private int id; private String name; private String className; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } } 

StudentEntity.java

package com.tutorialspoint.mapstructproject; public class StudentEntity { private int id; private String name; private String classVal; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassVal() { return classVal; } public void setClassVal(String classVal) { this.classVal = classVal; } } 

StudentMapper.java

package com.tutorialspoint.mapstructproject; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @Mapper(componentModel="spring") public interface StudentMapper { @Mapping(target="className", source="classVal") Student getModelFromEntity(StudentEntity student); @Mapping(target="classVal", source="className") StudentEntity getEntityFromModel(Student student); } 

StudentMapperTest.java

package com.tutorialspoint.mapstructproject; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.mapstructproject.StudentEntity; import com.tutorialspoint.mapstructproject.StudentMapper; import com.tutorialspoint.mapstructproject.Student; public class StudentMapperTest { private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class); @Test public void testEntityToModel() { StudentEntity entity = new StudentEntity(); entity.setClassVal("X"); entity.setName("John"); entity.setId(1); Student model = studentMapper.getModelFromEntity(entity); assertEquals(entity.getClassVal(), model.getClassName()); assertEquals(entity.getName(), model.getName()); assertEquals(entity.getId(), model.getId()); } @Test public void testModelToEntity() { Student model = new Student(); model.setId(1); model.setName("John"); model.setClassName("X"); StudentEntity entity = studentMapper.getEntityFromModel(model); assertEquals(entity.getClassVal(), model.getClassName()); assertEquals(entity.getName(), model.getName()); assertEquals(entity.getId(), model.getId()); } } 

Here is the stack trace:

java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.tutorialspoint.mapstructproject.StudentMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61) at com.tutorialspoint.mapstructproject.StudentMapperTest.<init>(StudentMapperTest.java:11) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:550) at org.junit.jupiter.engine.execution.ConstructorInvocation.proceed(ConstructorInvocation.java:56) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.api.extension.InvocationInterceptor.interceptTestClassConstructor(InvocationInterceptor.java:73) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:77) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestClassConstructor(ClassBasedTestDescriptor.java:355) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateTestClass(ClassBasedTestDescriptor.java:302) at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateTestClass(ClassTestDescriptor.java:79) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:280) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:272) at java.base/java.util.Optional.orElseGet(Optional.java:369) at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:271) at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:102) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:101) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:66) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84) at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.tutorialspoint.mapstructproject.StudentMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75) at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) ... 68 more 

UPDATE: Someone asked about the build file. I don't see a build file in my project, probably because the build never succeeds due to the error. Here is my file structure: enter image description here

6
  • No idea if this helps, but I only have these 2 mapstruct dependency lines when working with gradle. implementation 'org.mapstruct:mapstruct:1.4.2.Final' and annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'. Also can you check if the generated implementation classes are generated in the build folder. Commented Mar 28, 2022 at 20:35
  • @BenZegveld I started with only those two dependency lines and added the others in an attempt to solve the problem. As to checking if the generated implementation, I don't have a build file. I will edit the question to show the folder and file structure of the project. Commented Mar 29, 2022 at 15:28
  • What are you using to run the build/tests? ./gradlew build? or an IDE? Commented Mar 29, 2022 at 21:27
  • @BenZegveld I'm using Spring Tool Suite to run the builds and the tests. Commented Mar 30, 2022 at 15:23
  • 1
    Please add the solution if you found Commented Jul 18, 2022 at 20:11

1 Answer 1

1

For me works:

implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.5.3.Final' implementation group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final' compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24' annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.24' annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final' 

Taken from https://github.com/mapstruct/mapstruct-examples/blob/main/mapstruct-lombok/build.gradle

Optionally works without (please confirm it):

annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: '1.5.3.Final' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.