I have built a very simple bundle which has zero dependencies and imports no packages. Its only content is a CalculatorService interface and corresponding implementation class containing just a simple add(int a, int b) method.
I have created two PaxExam test containers for this bundle, one using a Karaf container, and the other an OSGI container. The Karaf container tests work fine, but the OSGI test container does not.
To clarify a bit... if I remove the injection of the CalculatorService into the OSGI test container, and directly instantiate the CalculatorServiceImpl class in my JUnit test case, it works fine. So the class from my simple bundle is visible to the OSGI test container.
Some questions:
Is there something I am missing in my pom.xml files to make this work?
Should I add more bundles to my OsgiTestClient.config() method?
Other ideas on what is keeping is very stripped down example from working?
The following is the stack trace I get when trying to inject the CalculatorService into my PaxExam OSGI test container.
org.ops4j.pax.swissbox.tracker.ServiceLookupException: gave up waiting for service info.xyz.common.Calculator
at org.ops4j.pax.swissbox.tracker.ServiceLookup.getService(ServiceLookup.java:199)
at org.ops4j.pax.swissbox.tracker.ServiceLookup.getService(ServiceLookup.java:136)
at org.ops4j.pax.exam.inject.internal.ServiceInjector.injectField(ServiceInjector.java:89)
at org.ops4j.pax.exam.inject.internal.ServiceInjector.injectDeclaredFields(ServiceInjector.java:69)
at org.ops4j.pax.exam.inject.internal.ServiceInjector.injectFields(ServiceInjector.java:61)
at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.createTest(ContainerTestRunner.java:61)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:68)
at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:37)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.invokeViaJUnit(JUnitProbeInvoker.java:124)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.findAndInvoke(JUnitProbeInvoker.java:97)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.call(JUnitProbeInvoker.java:73)
at org.ops4j.pax.exam.nat.internal.NativeTestContainer.call(NativeTestContainer.java:112)
at org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactor.invoke(AllConfinedStagedReactor.java:84)
at org.ops4j.pax.exam.junit.impl.ProbeRunner$2.evaluate(ProbeRunner.java:267)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.ops4j.pax.exam.junit.impl.ProbeRunner.run(ProbeRunner.java:98)
at org.ops4j.pax.exam.junit.PaxExam.run(PaxExam.java:93)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
The pom.xml for my simple bundle containing the Calculator service is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.xyz</groupId>
<artifactId>xyz-businessLogic</artifactId>
<version>0.0.1</version>
<packaging>bundle</packaging>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Import-Package>
</Import-Package>
<Export-Package>
info.xyz.common,
info.xyz.common.impl
</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
It is clear there are zero dependencies to my simple bundle, and the packages containing the Calculator and CalculatorImpl files are exported. Below is the blueprint.xml file for defining these services.
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance
http://aries.apache.org/xmlns/jpa/v1.0.0 http://aries.apache.org/xmlns/jpa/v1.0.0
http://aries.apache.org/xmlns/transactions/v1.0.0 http://aries.apache.org/xmlns/transactions/v1.0.0">
<bean id="calculatorService" class="info.xyz.common.impl.CalculatorImpl" />
<service ref="calculatorService" interface="info.xyz.common.Calculator" />
</blueprint>
Next, the pom.xml for my PaxExam OSGI container is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>info.xyz</groupId>
<artifactId>xyz-test-osgi</artifactId>
<version>0.0.1</version>
<parent>
<groupId>info.xyz</groupId>
<artifactId>xyz</artifactId>
<version>0.0.1</version>
<relativePath>../xyz</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<version>${pax.url.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-inject</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${ch.qos.logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>${javax.inject.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Testing target -->
<dependency>
<groupId>info.xyz</groupId>
<artifactId>xyz-businessLogic</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Needed if you use versionAsInProject() -->
<plugin>
<groupId>org.apache.servicemix.tooling</groupId>
<artifactId>depends-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>generate-depends-file</id>
<goals>
<goal>generate-depends-file</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally, the OSGI Test class is given as follows:
package info.xyz.test.osgi;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import info.xyz.common.Calculator;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.ProbeBuilder;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.osgi.framework.Constants;
#RunWith(PaxExam.class)
#ExamReactorStrategy(PerMethod.class)
public class OsgiTestClient
{
#Inject
protected Calculator _calculator;
#ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe)
{
System.out.println("TestProbeBuilder gets called");
probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*");
probe.setHeader(Constants.IMPORT_PACKAGE, "info.xyz.common");
probe.setHeader(Constants.IMPORT_PACKAGE, "info.xyz.common.impl");
return probe;
}
#Configuration
public Option[] config()
{
return new Option[] {
junitBundles(),
mavenBundle().groupId("info.xyz").artifactId("xyz-businessLogic").versionAsInProject(),
};
}
#Test
public void testAdd()
{
info.xyz.common.impl.CalculatorImpl calculator = new info.xyz.common.impl.CalculatorImpl();
int result = calculator.add(1, 2);
System.out.println("--- testing: ");
}
}
One final comment, if I comment out the code for injecting the CalculatorService, the code runs fine (as I am manually instantiating the CalculatorImpl class). So my test container does have both compile-time and run-time visibility to the Calculator interface and implementation. But injection of this service is not working.
And a final reminder, when I build a PaxExam Karaf Test Container, the injection of the Calculator service is successful and the test cases run fine. The error is when running the PaxExam OSGI container described in this post.
Yes, then it is that the blueprint stack/libraries are not installed by default in native container (but they are in Karaf).
You need to add this deps to your test pom:
<dependency>
<groupId>org.apache.aries</groupId>
<artifactId>org.apache.aries.util</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.proxy</groupId>
<artifactId>org.apache.aries.proxy.api</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.aries.proxy</groupId>
<artifactId>org.apache.aries.proxy.impl</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.api</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.core</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.core.compatibility</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.cm</artifactId>
<version>1.0.5</version>
</dependency>
And add the following bundles to your configuration in your test:
mavenBundle().groupId("org.apache.aries").artifactId("org.apache.aries.util").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.proxy").artifactId("org.apache.aries.proxy.api").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.proxy").artifactId("org.apache.aries.proxy.impl").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.blueprint").artifactId("org.apache.aries.blueprint.api").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.blueprint").artifactId("org.apache.aries.blueprint.cm").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.blueprint").artifactId("org.apache.aries.blueprint.core").versionAsInProject(),
mavenBundle().groupId("org.apache.aries.blueprint").artifactId("org.apache.aries.blueprint.core.compatibility").versionAsInProject().noStart(),
That should make the trick!
Related
I'm trying to create a Unit Test for a RestController in Spring Boot.
The Controller looks something like this:
#RestController()
#RequestMapping("/endpoint")
public class MyRestController {
#Autowired
private MyService service;
#GetMapping(value = "/{id}", produces = "application/json")
public ResponseEntity<String> findSomethingById(#PathVariable("id") String id, #RequestParam("param1")String param1) throws MyServiceException {
return ResponseEntity.ok(service.findSomethingById(id,param1));
}
And the Unit Test Class looks as follows:
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isA;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
#ExtendWith(SpringExtension.class)
#WebMvcTest(MyRestController.class)
public class MyRestControllerUnitTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private MyService service;
#Test
public void testGetSomethingId() throws Exception {
when(service.findSomethingById("1", "something")).thenReturn("found");
mockMvc.perform(get("/endpoint/1").param("param1","something"))
.andDo(print())
.andExpect(status().isOk());
// some more Expects
}
}
The issue is that both the service and the mockMvc Variables are null during the execution of the Test, but shouldn't be as they should get intantiated by Spring according to their Annotations according to my Understanding.
Looking for similair issues I found mostly issues where Junit4 and Junit5 got mixed togehter, which seems not to be my problem.
Sample question I found NullPointerException on controller when testing in SpringBoot API - Mocking
I'm using Spring Boot 2.7.4 as well as Java 11 and I added the spring-boot-starter-test dependency to my pom.xml for the Test dependencies
Below is the StackTrace I get when executing the Test with my IDEA:
java.lang.NullPointerException
at MyRestControllerUnitTest.findSomethingById(Line of when() as service is null and so is mockMvc)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:72)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
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:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Process finished with exit code -1
MainClass:
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>MyApplication</name>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<start-class>org.main.MyApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.main.MyApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The issue is coming because of the #Test annotation. You are using this annotation from the org.junit.Test package that is part of Junit4. Other annotations used in the test case are part of Junit5. So this is conflicting. Please use #Test annotation from the org.junit.jupiter.api.Test package and try again.
Also, remove this dependency from the pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
Adding to the answer by Rohit Agarwal, if you run
./mvw dependency:tree
You shall see transitive dependencies added by the spring-boot-test
\- org.springframework.boot:spring-boot-starter-test:jar:2.7.5:test
[INFO] +- org.springframework.boot:spring-boot-test:jar:2.7.5:test
[INFO] +- org.springframework.boot:spring-boot-test autoconfigure:jar:2.7.5:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.7.0:test
[INFO] | \- net.minidev:json-smart:jar:2.4.8:test
[INFO] | \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO] | \- org.ow2.asm:asm:jar:9.1:test
[INFO] +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] +- org.assertj:assertj-core:jar:3.22.0:test
[INFO] +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test <------------- Note this
[INFO] | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
JUnit 5 added by spring-boot-test as a transitive dependency as shown above.
Since Springboot 2.4 Junit4 Vintage engine is removed.
Hence you should use the Junit5 Test annotation org.junit.jupiter.api.Test
EDIT/UPDATE: Delegate IDE build/run actions to maven in IntelliJ settings solve the problem, but how to handle it without it?
I have the following problem:
I change something in my GetSthAspect class -> Then I run maven clean install (let's assume I have to always do clean install in my app) -> Run spring app -> And I see error:
java.lang.NoSuchMethodError: com.example.demo.aspects.GetSthAspect.aspectOf()Lcom/example/demo/aspects/GetSthAspect;
at com.example.demo.aspects.A.shouldDoSthAndCallMethodGetSth(A.java:15) ~[classes/:na]
at com.example.demo.aspects.AspectApp.main(AspectApp.java:18) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.10.jar:5.3.10]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar:5.3.10]
...
Then I stop app -> Again do maven clean install -> Again run app and now everything works fine.
It's always like that when I change something in my GetSthAspect class and I want to see my changes in app. Why is that? Why I have to build app -> run app -> stop app -> again build app and run it in order to my aspects works fine? What should I do to avoid this situation?
GetSthAspect
#Aspect
public class GetSthAspect {
Logger logger = LoggerFactory.getLogger(GetSthAspect.class);
#Around("call(* Ent.getSth())")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
logger.info("Inside around + 12:52");
Ent ent = (Ent) pjp.getTarget();
logger.info("Number: " + ent.getImportantNumber());
return pjp.proceed();
}
}
some class
public class AspectApp {
Logger logger = LoggerFactory.getLogger(AspectApp.class);
#GetMapping("/")
public String main() {
logger.info("Method main started + 11:01");
A a = new A();
a.shouldDoSthAndCallMethodGetSth(1);
}
}
A
public class A {
Logger logger = LoggerFactory.getLogger(A.class);
public void shouldDoSthAndCallMethodGetSth(Integer a) {
// ...
Ent ent = new Ent();
ent.setImportantNumber(1);
logger.info("Method A do sth and ...");
ent.getSth();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Testing</name>
<description>App for tests</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<complianceLevel>11</complianceLevel>
<source>11</source>
<target>11</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I can't tell where the problem is. normally you don't need to keep to maven clean install everytime you change something in your code.I suggest you use devtools maybe
I'm developing a Spring/MVC/Maven Web app in Eclipse. The use case is for the app to call Talend jobs on an adhoc basis with parameters. The user enters time frame information (years, months) on a form page. Everything worked (form, validation, model, configuration, error checking, etc) until I added the required Talend jars.
Some background - I successfully created a simple Web app in Eclipse that has the same use case. For this app, I needed to place the Talend jars into the WEB-INF\lib folder. For various reasons, I need to build a Web app that uses Spring/MVC/Maven technologies. I loaded all the required Talend jars into the WEB-INF\lib folder (exactly like I did with the previous Web app). I ran a successful Maven clean install. But running the embedded Tomcat (version 7.2.2) produced this error:
[ERROR] COMPILATION ERROR:
[ERROR]C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[13]
error: package talenddev1.job_gl_master_ad_hoc_0_3 does not exist
[ERROR]
C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[141,7]
error: cannot find symbol.
I then followed the steps from this site:
https://cleanprogrammer.net/adding-3rd-party-jar-to-maven-projects/
to add the third party jars to Maven projects (installed the jar into the local repository, added repository and dependency into the pom.xml, etc). Running the embedded Tomcat produced this error:
[ERROR] COMPILATION ERROR: [ERROR]
C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[155,16]
error: cannot access TalendJob
`
TalendJob` is located in the `UserValidator.java`:
job_GL_Master_Ad_Hoc TalendJob=new job_GL_Master_Ad_Hoc();
String[]
context=new String[] \{params...}
TalendJob.runJob(context);
I realize this maybe a Talend issue so I've been also working with the Talend community as well.
Any support will be greatly appreciated. Let me know if you need more information.
Thanks
Here is my pom.xml
`<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>TalendAdHoc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_master_ad_hoc_0_3</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/job_gl_master_ad_hoc_0_3.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_refresh_transaction_ad_hoc_0_2</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_refresh_transaction_ad_hoc_0_2.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_transaction_ad_hoc_0_2</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_transaction_ad_hoc_0_2.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_summary_ad_hoc_0_1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_summary_ad_hoc_0_1.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_project_ad_hoc_0_1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_project_ad_hoc_0_1.jar
</systemPath>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe
</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>`
And here is the UserValidator.java:
`package com.validator;
import java.util.Calendar;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.model.User;
import talenddev1.job_gl_master_ad_hoc_0_3.job_GL_Master_Ad_Hoc;
#Component
public class UserValidator implements Validator {
#Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
#Override
public void validate(Object obj, Errors err) {
User user = (User) obj;
Calendar cal = Calendar.getInstance();
set vars...
validation code...
error handling code...
}
job_GL_Master_Ad_Hoc talendJob = new job_GL_Master_Ad_Hoc();
String[] context = new String[] {
"--context_param Host_Analytics_CurrentYear=" + currentYear,
"--context_param Host_Analytics_CurrentYearStart=" +
currentYearStart,
"--context_param Host_Analytics_CurrentYearEnd=" + currentYearEnd,
"--context_param Host_Analytics_PreviousYear=" + previousYear,
"--context_param Host_Analytics_PreviousYearStart=" +
previousYearStart,
"--context_param Host_Analytics_PreviousYearEnd=" +
previousYearEnd,
"--context_param Host_Analytics_Transaction_Flag=" +
transactionFlag,
"--context_param Host_Analytics_Summary_Flag=" + summaryFlag,
"--context_param Host_Analytics_Project_Flag=" + projectFlag };
talendJob.runJob(context);
}
}`
I try to add mapstruct mapper in my Spring project.
I have a User entity. I need to show a list of users in the admin panel. For this, I did DTO UserForAdmin, mapper UserMapper and rest controller AdminRestController. When I try to get UserMapper I get errors.
I try two ways:
Mappers.getMapper(UserMapper.class)
I get error
java.lang.ClassNotFoundException: Cannot find implementation for
ru.project.mapper.UserMapper
Autowired
I get error
Error starting ApplicationContext. To display the conditions report
re-run your application with 'debug' enabled. 2019-07-17 15:47:07.886
ERROR 13652 --- [ restartedMain]
o.s.b.d.LoggingFailureAnalysisReporter :
*************************** APPLICATION FAILED TO START
Description:
Field userMapper in ru.project.controller.rest.AdminRestController
required a bean of type 'ru.project.mapper.UserMapper' that could not
be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'ru.project.mapper.UserMapper' in
your configuration.
Here my source code.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>ru.project</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project</name>
<description>The project is project of resourse for investors.</description>
<properties>
<java.version>12</java.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
My interface UserMapper:
package ru.project.mapper;
import java.util.List;
import org.mapstruct.Mapper;
import ru.project.domain.User;
import ru.project.dto.UserForAdmin;
#Mapper
//#Mapper(componentModel = "spring")
public interface UserMapper {
UserForAdmin UserToUserForAdmin(User user);
List<UserForAdmin> UserListToUserForAdminList(List<User> user);
}
My RestController:
package ru.project.controller.rest;
import java.util.List;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.project.dto.UserForAdmin;
import ru.project.mapper.UserMapper;
import ru.project.service.UserService;
#RestController
public class AdminRestController {
#Autowired
private UserService userService;
//#Autowired
//private UserMapper userMapper;
#GetMapping("/admin/users")
public List<UserForAdmin> findAllUsers(){
UserMapper userMapper = Mappers.getMapper(UserMapper.class);
return userMapper.UserListToUserForAdminList(userService.findAll());
}
}
I would like to use Awtowired.
You need to use #Mapper(componentModel="spring")
package ru.project.mapper;
import java.util.List;
import org.mapstruct.Mapper;
import ru.project.domain.User;
import ru.project.dto.UserForAdmin;
#Mapper(componentModel = "spring")
public interface UserMapper {
UserForAdmin UserToUserForAdmin(User user);
List<UserForAdmin> UserListToUserForAdminList(List<User> user);
}
and use below in AdminRestController
#Autowired
private UserMapper userMapper;
And i am assuming that User and UserForAdmin have same field names
After this run mvn clean compile and sources will be generated
An alternative answer to using annotation #Mapper(componentModel = "spring") would be to add the component model as compiler arguments in the plugin. The annotation works but the annoying thing may be having to add it to every mapper you create. A compiler argument you add once and works for all mappers in the project. Below is an example of a plugin definition with the componentModel compiler argument.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs>
</configuration>
</plugin>
I have tried this with versions 1.3.0.Final as well as 1.3.1.Final and sping boot 2.1.7/8/9
Use #Mapper(componentModel = "spring") - enable di.
Run mvn package command -it creates the implementation class.
#Autowired the mapper interface and use.
(method name start letter in java should be lower case)
There is one simple solution.
at your mapper class, use #Mapper(componentModel = "spring")
and the run mvn clean install command from terminal.
or in case of STS/ Eclipse, go to Project> Run As> maven clean
and then run Project> Run As> maven install
and your mapper Impl will be generated!
NOTE: Regarding to plugins
if you're using both of following dependencies then no need to use plugin in pom.xml file
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
and properties will be like;
<properties>
<java.version>11</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
</properties>
I solved it by adding scanBasePackages
#SpringBootApplication(scanBasePackages = {
"com.yourpackagepath.mapper"
})
Cannot find implementation for ru.project.mapper.UserMapper that means UserMapper must be implements by some class.
such as public Class UserMapperImple implements UserMapper {XXXXXXX}
then the Mappers.getMapper("UserMapper") will get UserMapperImple.
Consider defining a bean of type 'ru.project.mapper.UserMapper' in your configuration. that means #Mapper do not work; i advise you to check the spring-config.xml. may the ApplicationContext not scan this package.
hope to help you :)
I create a spring boot application from spring.io, then add CAS dependecy to pom file of project as below.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<version>${cas.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
after that start spring boot application, but CAS does not start in spring boot application.
what is the main problem in this usage of CAS?
Is the method of using the CAS at this way wrong?
_____________________________________________________________
finaly I solve my problem. I change the pom file as below
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<javaparser-core.version>3.12.0</javaparser-core.version>
<start.class>org.apereo.cas.web.CasWebApplication</start.class>
<start.class2>x.y.sso.SingleSignOnApplication</start.class2>
<h2.version>1.4.197</h2.version>
<cas.version>6.0.1</cas.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp-tomcat</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>${javaparser-core.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-configuration</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-configuration</artifactId>
<version>${cas.version}</version>
</dependency>
</dependencies>
<build>
<finalName>cas</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class2}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!--<recompressZippedFiles>false</recompressZippedFiles>-->
<archive>
<compress>false</compress>
<manifestFile>
${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp-tomcat/META-INF/MANIFEST.MF
</manifestFile>
</archive>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp-tomcat</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
then I create a Application class
#EnableDiscoveryClient
#SpringBootApplication(exclude = {
HibernateJpaAutoConfiguration.class,
JerseyAutoConfiguration.class,
JmxAutoConfiguration.class,
DataSourceAutoConfiguration.class,
DataSourceHealthIndicatorAutoConfiguration.class,
RedisAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class,
CassandraAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class})
#EnableConfigurationProperties({CasConfigurationProperties.class})
#EnableAsync
#EnableTransactionManagement(proxyTargetClass = true)
#EnableScheduling
public class SingleSignOnApplication {
public static void main(String[] args) {
SpringApplication.run(SingleSignOnApplication.class, args);
}
}
by this configuration, build maven goal and execution project by spring boot work correctly.
CAS is already a spring boot web application. Therefor have a look into cas-server-webapp-init-6.0.1.jar and look CasWebApplication.java.
#EnableDiscoveryClient
#SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class, JerseyAutoConfiguration.class, GroovyTemplateAutoConfiguration.class, JmxAutoConfiguration.class, DataSourceAutoConfiguration.class, DataSourceHealthIndicatorAutoConfiguration.class, RedisAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, CassandraAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class})
#EnableConfigurationProperties({CasConfigurationProperties.class})
#EnableAsync
#EnableTransactionManagement(proxyTargetClass=true)
#EnableScheduling
public class CasWebApplication
{
public static void main(String[] args)
{
Map<String, Object> properties = CasEmbeddedContainerUtils.getRuntimeProperties(Boolean.TRUE);
Banner banner = CasEmbeddedContainerUtils.getCasBannerInstance();
new SpringApplicationBuilder(new Class[] { CasWebApplication.class })
.banner(banner)
.web(WebApplicationType.SERVLET)
.properties(properties)
.logStartupInfo(true)
.contextClass(CasWebApplicationContext.class)
.run(args);
}
}
Remove the spring-boot-starter dependencies and CAS should start itself as a web application.
For further investigation I would recommend you to download the war from https://search.maven.org/artifact/org.apereo.cas/cas-server-webapp/6.0.1/war and inspect it with a tool like jd-gui.