Embedded tomcat not launching - spring-boot

I wanted to upgrade to spring boot 1.5.6 from boot 1.4.3.
I created a simple web project using the spring initializer and added a simple Controller class. My pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
RestApplication.java
package com.djcodes.testing.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}
EmployeeController.java
package com.djcodes.testing.rest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/employee")
public class EmployeeController {
#RequestMapping(method = { RequestMethod.GET }, value ="/welcome")
public String welcome() {
return "Welcome to employee controller";
}
}
When I try to Launch it doenst start and just builds
$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo-rest-api 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) > test-compile # demo-rest-api >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # demo-rest-api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # demo-rest-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # demo-rest-api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/dj/Github/test-frameworks-tools/demo-rest-api/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # demo-rest-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) < test-compile # demo-rest-api <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) # demo-rest-api ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-08-20 16:53:09.047 INFO 7690 --- [ main] c.djcodes.testing.rest.RestApplication : Starting RestApplication on lenovo with PID 7690 (/home/dj/Github/test-frameworks-tools/demo-rest-api/target/classes started by dj in /home/dj/Github/test-frameworks-tools/demo-rest-api)
2017-08-20 16:53:09.051 INFO 7690 --- [ main] c.djcodes.testing.rest.RestApplication : No active profile set, falling back to default profiles: default
2017-08-20 16:53:09.099 INFO 7690 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#72458578: startup date [Sun Aug 20 16:53:09 IST 2017]; root of context hierarchy
2017-08-20 16:53:09.762 INFO 7690 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-20 16:53:09.772 INFO 7690 --- [ main] c.djcodes.testing.rest.RestApplication : Started RestApplication in 0.966 seconds (JVM running for 4.05)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.740 s
[INFO] Finished at: 2017-08-20T16:53:09+05:30
[INFO] Final Memory: 28M/275M
[INFO] ------------------------------------------------------------------------
2017-08-20 16:53:09.942 INFO 7690 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#72458578: startup date [Sun Aug 20 16:53:09 IST 2017]; root of context hierarchy
2017-08-20 16:53:09.943 INFO 7690 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Has something change in spring-boot 1.5.6? Above works fine with 1.4.3
Thanks

Looks like it was spring boot version issues. Worked with 1.5.3.RELASE

Related

Error when executing java -jar in spring boot app but not compiling

When I compile the application it works perfect, also test are passed but then if I execute java -jat in the compiled jar i had the following error when trying to load a jpg resource:
XXX#XXXX MINGW64 /d/Proyectos/PetApp/Back (master)
$ mvn clean install
###########################################################
__________ __ __ _____ ____________________
\______ \ _____/ |__/ |_ / _ \\______ \______ \
| ___// __ \ __\ __\/ /_\ \| ___/| ___/
| | \ ___/| | | | / | \ | | |
|____| \___ >__| |__| \____|__ /____| |____|
\/ \/
###########################################################
2019-10-16 15:29:59,977 INFO [main] org.springframework.boot.StartupInfoLogger: Starting BackEndTestApplicationTests on gggarrido10 with PID 4436 (started by gggarrido in D:\Proyectos\PetApp\Back)
2019-10-16 15:29:59,980 DEBUG [main] org.springframework.boot.StartupInfoLogger: Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-10-16 15:29:59,981 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2019-10-16 15:30:04,187 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBConfig: H2 DDBB selected
2019-10-16 15:30:08,767 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Initializacion of the DDBB
2019-10-16 15:30:09,002 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: ADMIN stored on DDBB
2019-10-16 15:30:09,006 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: USER stored on DDBB
2019-10-16 15:30:09,017 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: VISITOR stored on DDBB
2019-10-16 15:30:09,237 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default AdminUser Created: AdminRoot/AdminPass246
2019-10-16 15:30:09,237 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Owner Created: Manolo Amelgas
2019-10-16 15:30:09,253 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Pet Created: Lucky
2019-10-16 15:30:14,019 INFO [main] org.springframework.boot.StartupInfoLogger: Started BackEndTestApplicationTests in 14.867 seconds (JVM running for 16.865)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.279 s - in com.mashosoft.backEndTest.BackEndTestApplicationTests
[INFO] Running com.mashosoft.backEndTest.security.service.Impl.AuthServiceImplTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.mashosoft.backEndTest.security.service.Impl.AuthServiceImplTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) # pettapp ---
[INFO] Building jar: D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) # pettapp ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) # pettapp ---
[INFO] Installing D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar to D:\Tools\.m2\repository\com\mashosoft\pettapp\V.RELEASE\pettapp-V.RELEASE.jar
[INFO] Installing D:\Proyectos\PetApp\Back\pom.xml to D:\Tools\.m2\repository\com\mashosoft\pettapp\V.RELEASE\pettapp-V.RELEASE.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.801 s
[INFO] Finished at: 2019-10-16T15:30:18+02:00
[INFO] Final Memory: 49M/331M
[INFO] ------------------------------------------------------------------------
XXX#XXXX MINGW64 /d/Proyectos/PetApp/Back (master)
$ cd target/
XXX#XXX MINGW64 /d/Proyectos/PetApp/Back/target (master)
$ java -jar pettapp-V.RELEASE.jar
###########################################################
__________ __ __ _____ ____________________
\______ \ _____/ |__/ |_ / _ \\______ \______ \
| ___// __ \ __\ __\/ /_\ \| ___/| ___/
| | \ ___/| | | | / | \ | | |
|____| \___ >__| |__| \____|__ /____| |____|
\/ \/
###########################################################
2019-10-16 15:30:33,466 INFO [main] org.springframework.boot.StartupInfoLogger: Starting BackEndTestApplication vV.RELEASE on gggarrido10 with PID 11360 (D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar started by gggarrido in D:\Proyectos\PetApp\Back\target)
2019-10-16 15:30:33,472 DEBUG [main] org.springframework.boot.StartupInfoLogger: Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-10-16 15:30:33,475 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2019-10-16 15:30:37,905 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBConfig: H2 DDBB selected
2019-10-16 15:30:41,331 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Initializacion of the DDBB
2019-10-16 15:30:41,792 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: ADMIN stored on DDBB
2019-10-16 15:30:41,795 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: USER stored on DDBB
2019-10-16 15:30:41,800 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: VISITOR stored on DDBB
2019-10-16 15:30:41,918 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default AdminUser Created: AdminRoot/AdminPass246
2019-10-16 15:30:41,919 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Owner Created: Manolo Amelgas
2019-10-16 15:30:41,963 ERROR [main] org.springframework.boot.SpringApplication: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DDBBInitializer': Invocation of init method failed; nested exception is com.mashosoft.backEndTest.exception.initialization.InitializationException
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at com.mashosoft.backEndTest.BackEndTestApplication.main(BackEndTestApplication.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: com.mashosoft.backEndTest.exception.initialization.InitializationException: null
at com.mashosoft.backEndTest.config.DDBB.DDBBInitializer.getTestPetImageAsMultipart(DDBBInitializer.java:123)
at com.mashosoft.backEndTest.config.DDBB.DDBBInitializer.init(DDBBInitializer.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
... 26 common frames omitted
This is the class:
package com.mashosoft.backEndTest.config.DDBB;
import com.mashosoft.backEndTest.component.ImageUploader;
import com.mashosoft.backEndTest.exception.errorcode.InitializationErrorCodes;
import com.mashosoft.backEndTest.exception.initialization.InitializationException;
import com.mashosoft.backEndTest.persistence.entity.Owner;
import com.mashosoft.backEndTest.persistence.entity.Pet;
import com.mashosoft.backEndTest.persistence.repository.OwnerRepository;
import com.mashosoft.backEndTest.persistence.repository.PetRepository;
import com.mashosoft.backEndTest.security.model.enums.Roles;
import com.mashosoft.backEndTest.security.persistence.entity.Role;
import com.mashosoft.backEndTest.security.persistence.entity.User;
import com.mashosoft.backEndTest.security.persistence.repository.RoleRepository;
import com.mashosoft.backEndTest.security.persistence.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
#Configuration
public class DDBBInitializer {
private static final Logger log = LoggerFactory.getLogger( DDBBInitializer.class );
#Autowired
private RoleRepository roleRepository;
#Autowired
private UserRepository userRepository;
#Autowired
private OwnerRepository ownerRepository;
#Autowired
private PetRepository petRepository;
#Autowired
private PasswordEncoder passwordEncoder;
#Value("${pettapp.config.default.username}")
private String defaultUsername;
#Value("${pettapp.config.default.email}")
private String defaultEmail;
#Value("${pettapp.config.default.password}")
private String defaultPassword;
#PostConstruct
private void init() {
log.info( "Initializacion of the DDBB" );
for (Roles role : Roles.values()) {
Role roleEntity = new Role( role );
roleRepository.save( roleEntity );
log.info( "Role: " + roleEntity.getRoleName() + " stored on DDBB" );
}
User defaultAdminUser = new User();
defaultAdminUser.setUserId( new BigDecimal( 1 ) );
defaultAdminUser.addOneRole( Roles.ADMIN );
defaultAdminUser.setUsername( defaultUsername );
defaultAdminUser.setPassword( passwordEncoder.encode( defaultPassword ) );
defaultAdminUser.setActive( true );
defaultAdminUser.setEmail( defaultEmail );
log.info( "Default AdminUser Created: " + defaultAdminUser.getUsername() + "/" + defaultPassword );
Owner adminOwnerProfile = new Owner();
adminOwnerProfile.setAddress( "Calle de jacinto NÂș6 Bajo B" );
adminOwnerProfile.setName( "Manolo" );
adminOwnerProfile.setSurname( "Amelgas" );
adminOwnerProfile.setDefaulter( false );
adminOwnerProfile.setTelephoneNumber( "678987656 " );
adminOwnerProfile.setUser( defaultAdminUser );
defaultAdminUser.setOwner( adminOwnerProfile );
log.info( "Default Owner Created: " + adminOwnerProfile.getName() + " " + adminOwnerProfile.getSurname() );
Pet testPet = new Pet();
testPet.setAlive( true );
testPet.setBitrh( new Date() );
testPet.setBreed( "Carlino" );
testPet.setFur( "White" );
testPet.setName( "Lucky" );
String imageUrl = ImageUploader.uploadFile( getTestPetImageAsMultipart() );
testPet.setPictureUrl( imageUrl );
testPet.setOwner( adminOwnerProfile );
adminOwnerProfile.addPet( testPet );
log.info( "Default Pet Created: " + testPet.getName() );
userRepository.save( defaultAdminUser );
}
private MultipartFile getTestPetImageAsMultipart() {
try {
File file;
try {
file = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getFile();
} catch (IOException e){
throw new InitializationException( InitializationErrorCodes.IMAGE_TEST_NOT_FOUND );
}
return new MockMultipartFile( "DogImageForTest.jpg", "DogImageForTest.jpg", "image/jpeg",
new FileInputStream( file ) );
} catch (Exception ignored) {
throw new InitializationException( InitializationErrorCodes.IMAGE_TEST_ERROR );
}
}
}
Why everything works fine but not when executing java -jar??
I dont know what to try because also in intellij when i press play or debug it works perfect.
The line that I know it is failing is the following:
file = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getFile();
I have check inside the jar file and the image is inside:
Thanks in advance
I found the solution. Within a jar I cant use .getFile(); because it expect to have a file withint the system. Intead I used:
fileStream = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getInputStream();
return new MockMultipartFile( "DogImageForTest.jpg", "DogImageForTest.jpg", "image/jpeg",fileStream );
With a stream it works perfect

Write Unit Test for API endpoint Controller

I have a spring controller which responds to API requests. I am trying to write Unit Tests for the controller class methods. I'll expose the controller code below:
AuditRestController.java
package com.audittrail.auditTrail.controller;
import com.audittrail.auditTrail.services.CreateAuditService;
import com.audittrail.auditTrail.services.GetAuditService;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.*;
import java.io.IOException;
#Controller("audit")
public class AuditRestController {
#Autowired
private GetAuditService getAuditService;
#Autowired
CreateAuditService createAuditService;
#JsonIgnoreProperties(ignoreUnknown = true)
#RequestMapping(value = "/audit/checkevent", method = RequestMethod.POST, consumes = {
MediaType.APPLICATION_JSON_VALUE })
#ResponseBody
public String create(#Validated #RequestBody String audi) throws IOException {
return createAuditService.createEvent(audi);
}
#JsonIgnoreProperties(ignoreUnknown = true)
#RequestMapping(value = "/audit/getevent", method = RequestMethod.POST, consumes = {
MediaType.APPLICATION_JSON_VALUE })
#ResponseBody
public String getObjectId(#Validated #RequestBody String audi) throws IOException {
return getAuditService.getEvent(audi);
}
}
One of the services that I am using is getAuditService.
GetAuditRestController.java
package com.audittrail.auditTrail.services;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.emc.leap.commons.client.web.LeapRestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
#Service
public class GetAuditService {
#Autowired
private final RestTemplate clientTemplate;
public GetAuditService(#Qualifier(LeapRestTemplate.LEAP_CLIENT_REST_TEMPLATE) RestTemplate clientTemplate) {
this.clientTemplate = clientTemplate;
}
public String getEvent(String audi) throws JsonProcessingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = null;
jsonNode = objectMapper.readTree(audi);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(headers);
String auditGetRequestURL = "some external url";
ResponseEntity<String> auditServiceGetRequest;
auditServiceGetRequest = this.clientTemplate.exchange(auditGetRequestURL, HttpMethod.GET, entity, String.class);
return auditServiceGetRequest.getBody().toString();
}
}
One thing to note is that this entire project does not have a main class. I am trying to build a non executable jar to be used in some other project. I have checked the jar without unit test cases and it is working fine. But I am also trying to write unit test cases.
This is bit of code that I am trying to execute.
AuditRestControllerTest.java
package com.audittrail.audittrail;
import com.google.gson.JsonObject;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.audittrail.auditTrail.controller.AuditObject;
import com.audittrail.auditTrail.controller.AuditRestController;
import com.audittrail.auditTrail.services.CreateAuditService;
import com.audittrail.auditTrail.services.GetAuditService;
#RunWith(SpringJUnit4ClassRunner.class)
#WebMvcTest(AuditRestController.class)
public class AuditRestControllerTest {
#Autowired
private MockMvc mockMvc;
#Autowired
AuditRestController auditRestController;
#MockBean
GetAuditService getAuditService;
#MockBean
CreateAuditService CreateAuditService;
#MockBean
AuditObject auditObject;
// #Before
// public void setUp() {
// mockMvc = MockMvcBuilders.standaloneSetup(new AuditRestController()).build();
// System.out.println("setup done");
// }
#Test
public void checkObjectId() throws Exception {
JsonObject demoInput = new JsonObject();
demoInput.addProperty("id", "1");
demoInput.addProperty("name", "Rajesh");
JsonObject demoResponse = new JsonObject();
demoResponse.addProperty("eventName", "Update");
demoResponse.addProperty("ObjectId", "12344");
Mockito.when(getAuditService.getEvent(demoInput.toString())).thenReturn(demoResponse.toString());
mockMvc.perform(MockMvcRequestBuilders.post("/audit/getevent").contentType(MediaType.APPLICATION_JSON)
.content(demoInput.toString()))
.andExpect(MockMvcResultMatchers.jsonPath("$.eventNmae", Matchers.is("Update")));
}
}
Shown Below is the pom.xml file that I have:
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>
<groupId>com.auditTrail</groupId>
<artifactId>audit-trail-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>audit-trail</name>
<description>Audit Service Integration</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.emc.leap</groupId>
<artifactId>leap-commons-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.emc.leap</groupId>
<artifactId>leap-commons-core</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
</project>
And finally here is the dependency tree for my maven:
[INFO] com.auditTrail:audit-trail-test:jar:0.0.1-SNAPSHOT
[INFO] +- com.google.code.gson:gson:jar:2.6.2:compile
[INFO] +- com.emc.leap:leap-commons-client:jar:1.1.0-SNAPSHOT:compile
[INFO] | +- org.springframework:spring-context:jar:4.3.17.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.3.17.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.17.RELEASE:compile
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.5.1:compile
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.3:compile
[INFO] | | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | | \- commons-codec:commons-codec:jar:1.9:compile
[INFO] | +- org.springframework:spring-web:jar:4.3.17.RELEASE:compile
[INFO] | +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] | \- com.eatthepath:fast-uuid:jar:0.1:compile
[INFO] +- com.emc.leap:leap-commons-core:jar:1.1.0-SNAPSHOT:compile
[INFO] | +- com.emc.dctm:security:jar:1.0.9:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.7.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.5:compile
[INFO] | +- commons-io:commons-io:jar:2.4:compile
[INFO] | +- org.springframework.security:spring-security-jwt:jar:1.0.9.RELEASE:compile
[INFO] | | \- org.bouncycastle:bcpkix-jdk15on:jar:1.56:compile
[INFO] | | \- org.bouncycastle:bcprov-jdk15on:jar:1.56:compile
[INFO] | +- org.springframework.security:spring-security-core:jar:4.2.6.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | \- net.jodah:expiringmap:jar:0.3.1:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- org.mockito:mockito-all:jar:1.9.5:test
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.1.1.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.1.1.RELEASE:test
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.1.1.RELEASE:test
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.1.RELEASE:test
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.1.RELEASE:test
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:test
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:test
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.1:test
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.1:test
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:test
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:test
[INFO] | | \- org.yaml:snakeyaml:jar:1.23:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.1.1.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.1.1.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.3:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.assertj:assertj-core:jar:3.11.1:test
[INFO] | +- org.mockito:mockito-core:jar:2.23.4:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.9.3:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.9.3:test
[INFO] | | \- org.objenesis:objenesis:jar:2.6:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:5.1.3.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.1.3.RELEASE:compile
[INFO] | +- org.springframework:spring-test:jar:5.1.3.RELEASE:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.6.2:test
[INFO] \- org.springframework:spring-tx:jar:5.1.5.RELEASE:compile
[INFO] \- org.springframework:spring-beans:jar:5.1.5.RELEASE:compile
And Now finally here is the error that I am getting after mvn:install
-<testcase time="0.017" name="initializationError" classname="com.audittrail.audittrail.AuditRestControllerTest">
<error type="java.lang.NoClassDefFoundError" message="org/springframework/web/servlet/config/annotation/WebMvcConfigurer">java.lang.NoClassDefFoundError: org/springframework/web/servlet/config/annotation/WebMvcConfigurerat org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTypeExcludeFilter.<clinit>(WebMvcTypeExcludeFilter.java:58)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source)at org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer.instantiateTypeExcludeFilter(TypeExcludeFiltersContextCustomizer.java:66)at org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer.instantiateTypeExcludeFilters(TypeExcludeFiltersContextCustomizer.java:55)at org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer.<init>(TypeExcludeFiltersContextCustomizer.java:48)at org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory.createContextCustomizer(TypeExcludeFiltersContextCustomizerFactory.java:47)at org.springframework.test.context.support.AbstractTestContextBootstrapper.getContextCustomizers(AbstractTestContextBootstrapper.java:404)at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:376)at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:312)at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:265)at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:99)at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:139)at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)at java.lang.reflect.Constructor.newInstance(Unknown Source)at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.config.annotation.WebMvcConfigurerat java.net.URLClassLoader.findClass(Unknown Source)at java.lang.ClassLoader.loadClass(Unknown Source)at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)at java.lang.ClassLoader.loadClass(Unknown Source)... 41 more </error>
I am pretty new to this. I have tried to learn and write the tests and the above code. I am badly stuck at this. Any help is welcome.
Also I do not have any ApplicationContext defined for the Controller as I am using it as external jar in another project. Is it possible to create the tests in the same way.
We only need to test this one controller with two endpoints.
Thanks in advance.
Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.config.annotation.WebMvcConfigurer: looks like your missing some dependencies.
This class should be contained in spring-webmvc
This error arose because I had not involved the spring-webmvc dependency in my pom.xml.
The reason I was not including it was beacuse I was getting error "AbstractMethodError".
But later I found out that the main problem was because I was mixing up spring-boot and spring together.
Spring-starter-test comes with spring-boot and hence the whole project was collapsing.
Thanks to people who responded.

Spring Boot 1.5.3 web application terminate immediately after started

After started the Spring Boot Project running on 1.5.3.RELEASE the logs running then force stop
Anyone have any idea why 1.5.3 is stop, I tried 1.4.6 The started successfully
Pom.xml
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The Application
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
#SpringBootApplication
#Controller
#EnableAutoConfiguration
public class DemoApplication {
#RequestMapping("/")
#ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Logs
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------------------------------------------------
---
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ---------------------------------------------------------------------
---
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-
compile # demo >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Workspace\spring\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile # demo <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) # demo ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)
2017-05-10 15:25:09.638 INFO 11112 --- [ main] com.example.DemoApplication : Starting DemoApplication on WutikornMat with PID 11112 (D:\Workspace\sp
ring\demo\target\classes started by sutee.cha in D:\Workspace\spring\demo)
2017-05-10 15:25:09.643 INFO 11112 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default
2017-05-10 15:25:09.739 INFO 11112 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigAppli
cationContext#3a723bfc: startup date [Wed May 10 15:25:09 ICT 2017]; root of context hierarchy
2017-05-10 15:25:10.599 INFO 11112 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-05-10 15:25:10.617 INFO 11112 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.599 seconds (JVM running for 5.332)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.069 s
[INFO] Finished at: 2017-05-10T15:25:10+07:00
[INFO] Final Memory: 33M/398M
[INFO] ------------------------------------------------------------------------
2017-05-10 15:25:10.751 INFO 11112 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicat
ionContext#3a723bfc: startup date [Wed May 10 15:25:09 ICT 2017]; root of context hierarchy
2017-05-10 15:25:10.754 INFO 11112 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Then I run the jar via command line outside IDE
2017-05-10 15:38:57.630 INFO 7532 --- [ main] com.example.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on WutikornMat with PID 7532 (D
:\Workspace\spring\demo\target\demo-0.0.1-SNAPSHOT.jar started by sutee.cha in D:\Workspace\spring\demo\target)
2017-05-10 15:38:57.638 INFO 7532 --- [ main] com.example.DemoApplication : No active profile set, falling back to default profiles: default
2017-05-10 15:38:57.765 INFO 7532 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplic
ationContext#255316f2: startup date [Wed May 10 15:38:57 ICT 2017]; root of context hierarchy
2017-05-10 15:38:58.973 INFO 7532 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-05-10 15:38:58.997 INFO 7532 --- [ main] com.example.DemoApplication : Started DemoApplication in 1.888 seconds (JVM running for 2.455)
2017-05-10 15:38:59.000 INFO 7532 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicati
onContext#255316f2: startup date [Wed May 10 15:38:57 ICT 2017]; root of context hierarchy
2017-05-10 15:38:59.003 INFO 7532 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
So you can see the Setup and Example is exactly the same as spring docs
What I missed on 1.5.3
Thanks in advance
If you look at the current example there is no #SpringBootApplication annotation, but
#RestController
#EnableAutoConfiguration
public class DemoApplication {
This runs out of the box. If I start your example, I get
** WARNING ** : Your ApplicationContext is unlikely to start due to a #ComponentScan of the default package.
If I move the class from the default package to any package - let's say demo - the application starts correctly.

Maven is not launching the browser for selenium

I am doing a basic test in Selenium and I want it to execute from maven I am not getting any error but browser is not launched.and in TESTS section I get
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
my src/Test/java/Test.java is
public class Test {
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.gecko.driver","C:/Users/swkv8851/Downloads/geckodriver-v0.15.0-win32/geckodriver.exe");
Open open=new Open();
open.Opengmail();
}
}
and my src/main/java.Open.java is:
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Open {
WebDriver driver=new FirefoxDriver();
public void Opengmail() throws Exception{
driver.get("http://google.com");
driver.findElement(By.linkText("Gmail")).click();
Thread.sleep(5000);
driver.findElement(By.name("Email")).sendKeys ("somevalues");
String st=driver.findElement(By.xpath("//a[#class='need-help']")).getText();
System.out.println(st);
Assert.assertEquals("Find my account", st);
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.name("Passwd")).sendKeys ("some string");
driver.findElement(By.id("signIn")).click();
}
}
MY POM file:
<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>SeleniumTestWithMaven</groupId>
<artifactId>SeleniumTestWithMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SeleniumTestWithMaven</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
</project>
OUTPUT of execution:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SeleniumTestWithMaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # SeleniumTestWithMaven ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # SeleniumTestWithMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # SeleniumTestWithMaven ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # SeleniumTestWithMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # SeleniumTestWithMaven ---
[INFO] Surefire report directory: C:\Personal\learn_selenium\workspace\SeleniumTestWithMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.294 s
[INFO] Finished at: 2017-04-04T16:23:45+05:30
[INFO] Final Memory: 9M/114M
[INFO] ------------------------------------------------------------------------
how can I my invoke my browser and perform test.
Selenium Version >3.0 requires GeckoDriver to run Firefox found # https://github.com/mozilla/geckodriver/releases
There is no test in your code (no #Test annotation)
You need something like this :
public class Test {
#Test
public void myTest() {
System.setProperty("webdriver.gecko.driver","C:/Users/swkv8851/Downloads/geckodriver-v0.15.0-win32/geckodriver.exe");
Open open=new Open();
open.Opengmail();
}
}

Jersey Singleton Using Spring: Method cannot be cast to Constructor

I am trying to use Spring to autowire Jersey but I'm getting the weirdest exception as soon as one of the methods gets called.
Here is my resource:
package fungle.funfinder.rest.resource;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import fungle.common.hdao.model.ByteId;
import fungle.common.hdao.model.OnError;
import fungle.common.hdao.util.CascadeUtils;
import fungle.common.hdao.util.FetchUtils;
import fungle.funfinder.data.dao.ActivityTypeDAO;
import fungle.funfinder.data.entity.ActivityType;
#Path("activityType")
#Singleton
#Service
public class ActivityTypeResource {
private ActivityTypeDAO activityTypeDAO;
public ActivityTypeResource() {
}
public ActivityTypeResource(ActivityTypeDAO activityTypeDAO) {
this.activityTypeDAO=activityTypeDAO;
}
public ActivityTypeDAO getActivityTypeDAO() {
return activityTypeDAO;
}
#Autowired
public void setActivityTypeDAO(ActivityTypeDAO activityTypeDAO) {
this.activityTypeDAO = activityTypeDAO;
}
#GET
#Path("/all")
#Produces(value={MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public List<ActivityType> getAll() {
Map<ByteId, ActivityType> types = activityTypeDAO.getAll(FetchUtils.always(),OnError.CONTINUE,null,null);
return new ArrayList<>(types.values());
}
#POST
public Response post(ActivityType activityType,#Context UriInfo uriInfo) {
ActivityType existing = activityTypeDAO.getByName(activityType.getName());
if (existing==null) {
activityTypeDAO.save(activityType, CascadeUtils.always(), OnError.ATTEMPT_UNDO);
existing = activityTypeDAO.getByName(activityType.getName());
if (existing !=null) {
// success
URI uri = uriInfo.getAbsolutePathBuilder().path(existing.getName()).build();
return Response.created(uri).build();
} else {
// failed to save.
return Response.serverError().build();
}
} else {
// already exists.
return Response.notModified("An activity type with that name already exists.").entity(existing).build();
}
}
#GET
#Path("/{name}")
public ActivityType getByName(#PathParam("name") String name) {
ActivityType existing = activityTypeDAO.getByName(name);
return existing;
}
}
Here is my maven dependency:tree:
[INFO] fungle:fungle.funfinder.data:jar:0.0.1-SNAPSHOT
[INFO] +- fungle:fungle.common.core:jar:0.0.1-SNAPSHOT:compile
[INFO] +- fungle:fungle.common.hdao:jar:0.0.1-SNAPSHOT:compile
[INFO] +- org.apache.avro:avro:jar:1.7.6:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile
[INFO] | +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] | +- org.xerial.snappy:snappy-java:jar:1.0.5:compile
[INFO] | \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] | \- org.tukaani:xz:jar:1.0:compile
[INFO] +- org.apache.commons:commons-math3:jar:3.2:compile
[INFO] +- org.geotools:gt-referencing:jar:11.1:compile
[INFO] | +- java3d:vecmath:jar:1.3.2:compile
[INFO] | +- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] | +- org.geotools:gt-metadata:jar:11.1:compile
[INFO] | | \- org.geotools:gt-opengis:jar:11.1:compile
[INFO] | +- jgridshift:jgridshift:jar:1.0:compile
[INFO] | \- javax.media:jai_core:jar:1.1.3:compile
[INFO] +- org.geotools:gt-geometry:jar:11.1:compile
[INFO] | +- net.java.dev.jsr-275:jsr-275:jar:1.0-beta-2:compile
[INFO] | \- org.geotools:gt-main:jar:11.1:compile
[INFO] | +- org.geotools:gt-api:jar:11.1:compile
[INFO] | +- com.vividsolutions:jts:jar:1.13:compile
[INFO] | \- org.jdom:jdom:jar:1.1.3:compile
[INFO] +- org.geotools:gt-epsg-hsql:jar:11.1:compile
[INFO] | \- org.hsqldb:hsqldb:jar:2.2.8:compile
[INFO] +- org.springframework:spring-aspects:jar:4.0.2.RELEASE:compile
[INFO] | \- org.aspectj:aspectjweaver:jar:1.7.4:compile
[INFO] +- org.springframework:spring-context:jar:4.0.2.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:4.0.2.RELEASE:test
[INFO] +- org.springframework:spring-aop:jar:4.0.2.RELEASE:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-tx:jar:4.0.2.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:4.0.2.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] +- org.springframework:spring-beans:jar:4.0.2.RELEASE:compile
[INFO] +- org.springframework:spring-expression:jar:4.0.2.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:4.0.2.RELEASE:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.0.2.RELEASE:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.2:compile
[INFO] | \- ch.qos.logback:logback-core:jar:1.1.2:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] +- commons-cli:commons-cli:jar:20040117.000000:compile
[INFO] +- com.google.guava:guava:jar:17.0:compile
[INFO] +- org.apache.commons:commons-collections4:jar:4.0:compile
[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.1:compile
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.apache.hadoop:hadoop-client:jar:2.0.0-cdh4.5.0:compile
[INFO] | +- org.apache.hadoop:hadoop-common:jar:2.0.0-cdh4.5.0:compile
[INFO] | | +- org.apache.commons:commons-math:jar:2.1:compile
[INFO] | | +- xmlenc:xmlenc:jar:0.52:compile
[INFO] | | +- commons-net:commons-net:jar:3.1:compile
[INFO] | | +- commons-el:commons-el:jar:1.0:runtime
[INFO] | | +- org.apache.hadoop:cloudera-jets3t:jar:2.0.0-cdh4.5.0:compile
[INFO] | | +- org.mockito:mockito-all:jar:1.8.5:compile
[INFO] | | +- org.apache.hadoop:hadoop-auth:jar:2.0.0-cdh4.5.0:compile
[INFO] | | \- com.jcraft:jsch:jar:0.1.42:compile
[INFO] | +- org.apache.hadoop:hadoop-hdfs:jar:2.0.0-cdh4.5.0:compile
[INFO] | +- org.apache.hadoop:hadoop-mapreduce-client-app:jar:2.0.0-cdh4.5.0:compile
[INFO] | | +- org.apache.hadoop:hadoop-mapreduce-client-common:jar:2.0.0-cdh4.5.0:compile
[INFO] | | | +- org.apache.hadoop:hadoop-yarn-client:jar:2.0.0-cdh4.5.0:compile
[INFO] | | | \- org.apache.hadoop:hadoop-yarn-server-common:jar:2.0.0-cdh4.5.0:compile
[INFO] | | +- org.apache.hadoop:hadoop-mapreduce-client-shuffle:jar:2.0.0-cdh4.5.0:compile
[INFO] | | \- org.jboss.netty:netty:jar:3.2.4.Final:compile
[INFO] | +- org.apache.hadoop:hadoop-yarn-api:jar:2.0.0-cdh4.5.0:compile
[INFO] | +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:2.0.0-cdh4.5.0:compile
[INFO] | | \- org.apache.hadoop:hadoop-yarn-common:jar:2.0.0-cdh4.5.0:compile
[INFO] | +- org.apache.hadoop:hadoop-mapreduce-client-jobclient:jar:2.0.0-cdh4.5.0:compile
[INFO] | \- org.apache.hadoop:hadoop-annotations:jar:2.0.0-cdh4.5.0:compile
[INFO] \- org.apache.hbase:hbase:jar:0.94.6-cdh4.5.0:compile
[INFO] +- com.yammer.metrics:metrics-core:jar:2.1.2:compile
[INFO] +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] | +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] | +- commons-digester:commons-digester:jar:1.8:compile
[INFO] | | \- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] | \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] +- com.github.stephenc.high-scale-lib:high-scale-lib:jar:1.1.1:compile
[INFO] +- commons-codec:commons-codec:jar:20041127.091804:compile (version managed from 1.4)
[INFO] +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] +- commons-lang:commons-lang:jar:2.5:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] +- org.apache.zookeeper:zookeeper:jar:3.4.5-cdh4.5.0:compile
[INFO] +- org.apache.thrift:libthrift:jar:0.9.0:compile
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.3.1:compile (version managed from 4.1.3)
[INFO] | \- org.apache.httpcomponents:httpcore:jar:4.1.3:compile
[INFO] +- org.jruby:jruby-complete:jar:1.6.5:compile
[INFO] +- org.mortbay.jetty:jetty:jar:6.1.26.cloudera.2:compile
[INFO] +- org.mortbay.jetty:jetty-util:jar:6.1.26.cloudera.2:compile
[INFO] +- org.mortbay.jetty:jsp-2.1:jar:6.1.14:compile
[INFO] | \- org.eclipse.jdt:core:jar:3.1.1:compile
[INFO] +- org.mortbay.jetty:jsp-api-2.1:jar:6.1.14:compile
[INFO] +- org.codehaus.jackson:jackson-jaxrs:jar:1.8.8:compile
[INFO] +- org.codehaus.jackson:jackson-xc:jar:1.8.8:compile
[INFO] +- tomcat:jasper-compiler:jar:5.5.23:runtime
[INFO] +- tomcat:jasper-runtime:jar:5.5.23:runtime
[INFO] +- org.jamon:jamon-runtime:jar:2.3.1:compile
[INFO] +- com.google.protobuf:protobuf-java:jar:2.4.0a:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.1:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] \- stax:stax-api:jar:1.0.1:compile
I am not sure if this is relevant or not, but I was having some jersey jar conflicts a while back, my hadoop jars were pulling in some jersey artifacts so I threw some exclusions in my pom as follows:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<artifactId>
jersey-test-framework-grizzly2
</artifactId>
<groupId>
com.sun.jersey.jersey-test-framework
</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-guice</artifactId>
<groupId>com.sun.jersey.contribs</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-server</artifactId>
<groupId>com.sun.jersey</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-core</artifactId>
<groupId>com.sun.jersey</groupId>
</exclusion>
<exclusion>
<artifactId>jersey-json</artifactId>
<groupId>com.sun.jersey</groupId>
</exclusion>
</exclusions>
</dependency>
So when I run my web app, everything seems to be fine until that resource is called when my jsp page loads. Here is the stack trace:
SEVERE: Servlet.service() for servlet [jersey] in context with path [/fungle.funfinder.web] threw exception [A MultiException has 3 exceptions. They are:
1. java.lang.ClassCastException: java.lang.reflect.Method cannot be cast to java.lang.reflect.Constructor
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of fungle.funfinder.rest.resource.ActivityTypeResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on fungle.funfinder.rest.resource.ActivityTypeResource
] with root cause
java.lang.ClassCastException: java.lang.reflect.Method cannot be cast to java.lang.reflect.Constructor
at org.glassfish.jersey.server.spring.AutowiredInjectResolver.createSpringDependencyDescriptor(AutowiredInjectResolver.java:114)
at org.glassfish.jersey.server.spring.AutowiredInjectResolver.getBeanFromSpringContext(AutowiredInjectResolver.java:97)
at org.glassfish.jersey.server.spring.AutowiredInjectResolver.resolve(AutowiredInjectResolver.java:92)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:214)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:244)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:360)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:114)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:102)
at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:153)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2151)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:641)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:626)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1025)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
I can also post any of my app contexts and/or web xml file, it's just that this post is already pretty long and I think this might be a classpath issue. Let me know if you want to see those.
Please tell me what I'm doing wrong.
==================================
Edit: I just moved the autowired to the constructor and now it is working without exception. Still, I might need to use a setter method in the future for various reasons. Would somebody please tell me why it fails when I autowire the setter? Is this a bug?
This was due to a bug in Jersey (JERSEY-2681). This bug was fixed in Jersey 2.16, which was released in 2015. It was fixed in Jersey GitHub pull request #115.
The best fix therefore would be to update to Jersey 2.16 or later.
The issue was that the Jersey code re-implements some of the Spring autowiring logic, but was missing the setter method case. The exception thrown in the AutowiredInjectResolver class was because method autowiring case wasn't being taken into account; if the autowired element wasn't a field, the code was assuming it's a constructor. So in the case of an autowired method, a ClassCastException was thrown.
private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) {
AnnotatedElement annotatedElement = injectee.getParent();
if (annotatedElement.getClass().isAssignableFrom(Field.class)) {
return new DependencyDescriptor((Field) annotatedElement,
!injectee.isOptional());
} else {
return new DependencyDescriptor(
new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional());
}
}
If your code is stuck on an older version of Jersey and you don't want to use constructor injection, field injection does still work. So you could annotate the field with #Autowired and everything should work. You could still keep the setter around if you still want or need it.
#Autowired
private ActivityTypeDAO activityTypeDAO;
public void setActivityTypeDAO(ActivityTypeDAO activityTypeDAO) {
this.activityTypeDAO = activityTypeDAO;
}

Resources