#TestExecutionListeners is not present for class - spring

I will try to test one of methods in my endpoint (spring 3.1, junit 4.11) Here are my codes:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns: p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schem...ng-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schem...ring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="app.controller, app.samples" />
<context:annotation-config/>
<annotation-driven />
</beans>
and test class:
package app.tests;
import app.samples.TableEndpoint;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.test.context.ContextConfigurat ion;
import org.springframework.test.context.junit4.SpringJUni t4ClassRunner;
#ContextConfiguration(locations = {"classpath:/WEB-INF/applicationContext.xml"})
#RunWith(SpringJUnit4ClassRunner.class)
public class TableTest {
public TableTest() {
}
#Autowired
TableEndpoint tableEndpoint;
#Test
public void testTableEndpoint(){
String result = tableEndpoint.getDane().get(0);
String expResult = "Learn python in 7 days";
if(!result.equals(expResult)){
fail("not equals");
}
assertTrue(result.equals(expResult));
}
}
If I run test I have got :
org.springframework.test.context.TestContextManage r retrieveTestExecutionListeners<br>
INFO: #TestExecutionListeners is not present for class [class app.tests.TableTest]: using defaults.
I searched about it but didn't find some informations. Thanks for help!

You miss the TestExecutionListeners. Add this annotation to your class
#TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
#ContextConfiguration(locations = {"classpath:/WEB-INF/applicationContext.xml"})
#RunWith(SpringJUnit4ClassRunner.class)
public class TableTest {
...
}

Related

spring The request sent by the client was syntactically incorrect() issue

I am getting exception mentioned in subject line. Could you please explain what is wrong:
package mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping("/req")
public class Req {
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public String get(#RequestBody String body) {
return body;
}
}
SpringMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--It tells the Spring framework to look for annotations and then do appropriate dependency injections.-->
<context:annotation-config/>
<!--used to provide base package to scan components-->
<context:component-scan base-package="mvc"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
When i tries to access http://localhost:8080/SpringMVC/req
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect ().
Apache Tomcat/7.0.16
Kindly help me in figure out the cause of this issue.
Thanks,
Sandip
Change method to POST and send some data with HTTP POST so the (#RequestBody String body) binding works.
You could use SoapUi or HTTP Requester plugin for browsers to conveniently send HTTP POST.
Otherwise define method as:
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public String get() {
return "Some string";
}

SpringMVC+AspectJ not called

Hi I read all previous threads on my issue and i applied all suggestions, but i haven't found a solution.
In my springMVC+AspectJ config, controller works fine but the pointcut is not performed.
Here my configs and code:
-- /WEB-INF/web.xml ---
<web-app>
<display-name>MODULE-WEB</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
**
-- /WEB-INF/ApplicationContext.xml -----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
>
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="it.max.test"/>
<!-- AOP -->
<aop:aspectj-autoproxy/>
<!-- EJB -->
<bean id="testService" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="java:app/MODULE-EJB/TestService"/>
<property name="businessInterface" value="it.max.test.services.ITestService"/>
</bean>
</beans>
**
-- Monitor.java ---
package it.max.test.security;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
#Component
#Target(value={ElementType.METHOD, ElementType.TYPE})
#Retention(value=RetentionPolicy.RUNTIME)
public #interface Monitor {
}
**
-- AuthorizationInterceptor.java --
package it.max.test.security;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.stereotype.Component;
#Aspect
#Component
public class AuthorizationInterceptor {
#Before(value="#within(it.max.test.security.Monitor) || #annotation(it.max.test.security.Monitor)")
public void before(JoinPoint jp){
Object[] paramValues=jp.getArgs();
String[] paramNames=((CodeSignature)jp.getStaticPart().getSignature()).getParameterNames();
for(int i=0;i<paramValues.length;i++){
System.out.println("["+paramNames[i]+"]:["+paramValues[i]+"]");
}
}
}
**
--TestController.java---
package it.max.test.controllers;
import it.max.test.security.Monitor;
import it.max.test.services.ITestService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#Autowired
private ITestService testService;
#Monitor
#RequestMapping("/test.view")
protected ModelAndView test(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
ModelAndView mv=new ModelAndView();
testService.test("AAA");
mv.setViewName("test");
return mv;
}
}
Your annotated method is protected, but it should be public.
Spring manual, chapter 9.2.3 says:
Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!
If you want to match protected or private methods, use AspectJ via LTW.

404 Error on using Spring AOP Logging

I was trying to add AOP logger to the existing Spring(v3.1.3) application. Below is the code for the same.
Application launches successfully and am able to login. But, once the flow reaches the Controller that's specified in the ApplicationLogger.java, am getting 404 error in the screen and unfortunately aint getting any error trace in the console. Not getting any clue.
Kindly help me out to find where i have gone wrong.
TIA,
Arun
SERVLET.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" >
<context:annotation-config />
<mvc:annotation-driven />
<!-- Enables the caching through annotations -->
<cache:annotation-driven />
<aop:aspectj-autoproxy />
<bean id="appLogger"
class="com.pmc.crm.risk.util.ApplicationLogger" />
.
.
.
ApplicationLogger.java
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class ApplicationLogger
{
/**
* Log method entry.
*
* #param joinPoint
*/
#Before("execution(* com.pmc.crm.risk.mvc.controller.SearchController.loadParamSearch(..))")
public void logEntry(final JoinPoint joinPoint)
{
System.out.println("*--*-*-*-* LOG ENTRY -*-*-*");
log("Entering method " + joinPoint.getSignature().getName() + "...");
}
/**
* Log method exit.
*
* #param joinPoint
*/
#After("execution(* com.pmc.crm.risk.mvc.controller.SearchController.loadParamSearch(..))")
public void logExit(final JoinPoint joinPoint)
{
System.out.println("*--*-*-*-* LOG EXIT -*-*-*");
log("Exiting method " + joinPoint.getSignature().getName() + ".");
}
SEARCHCONTROLLER.java
package com.pmc.crm.risk.mvc.controller;
import ....
#Controller
public class SearchController extends AbstractPolicyController{
private static final Logger LOG = Logger.getLogger(SearchController.class);
#RequestMapping(value = "/loadParamSearch.htm", method = RequestMethod.GET)
public ModelAndView loadParamSearch(HttpServletRequest request, HttpServletResponse response,ModelMap model) throws Exception{
try
{
System.out.println("...");
}
.
.
AbstractPolicyController was actually implementing an interface and servlet couldnt inject a proxy for the same (Investigating on the reason.) On removing the 'implements interfaceName' it worked good. Anyone who know the reason may pls let me know.

injection of property in xml fails (spring-ws config)

i'm using Spring-WS and have the following spring-ws-servlet.xml file.
The injection of defaultURI and marshaller doesn't work because when i get to the method in the client of the service these properties are null.
What happens is that the setters are being called with correct values, but in the method of the client getSum() these values are null. What could be wrong?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<context:component-scan base-package="com.coral.project.endpoints"/>
<sws:annotation-driven />
<sws:dynamic-wsdl id="test" portTypeName="TestCase" locationUri="/testService/"
targetNamespace="http://www.example.org/schemasDef/test/definitions">
<sws:xsd location="/WEB-INF/schemasDef/test.xsd"/>
</sws:dynamic-wsdl>
<bean id="springWSClient" class="com.coral.project.endpoints.SpringWSClient">
<property name="defaultUri" value="http://localhost:8080/parking/springServices/testService"/>
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="com.coral.project.entity.Street"/>
</oxm:jaxb2-marshaller>
</beans>
i get the exception:
Exception while calling encodeEnd on component : {Component-Path : [Class: org.ajax4jsf.component.AjaxViewRoot,ViewId: /appealConversionStatusReport.jsp][Class: org.apache.myfaces.custom.div.Div,Id: j_id_jsp_1406177460_4][Class: com.exadel.htmLib.components.UITable,Id: j_id_jsp_1406177460_5][Class: com.exadel.htmLib.components.UITbody,Id: j_id_jsp_1406177460_6][Class: org.apache.myfaces.component.html.ext.HtmlInputHidden,Id: j_id_jsp_546672833_0]}
Caused by:
java.lang.IllegalStateException - No marshaller registered. Check configuration of WebServiceTemplate.
the client:
package com.coral.project.endpoints;
import java.io.IOException;
import java.io.StringWriter;
import javax.inject.Inject;
import javax.xml.soap.SOAPException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.xml.transform.StringSource;
import com.coral.project.dao.ifc.StreetDao;
import com.coral.project.entity.Street;
import com.coral.utils.SpringUtils;
public class SpringWSClient extends WebServiceGatewaySupport {
public void getSum() throws SOAPException, IOException, TransformerException {
StreetDao streetDao = SpringUtils.getBean(StreetDao.class);
Street street = streetDao.findById(1);
getWebServiceTemplate().marshalSendAndReceive(
"http://localhost:8080/parking/springServices/testService",street);
}
}
Are you new'ing the SpringWSClient instance in your code?
Also, for the streetDao, you shouldn't need to use SpringUtils.getBean. Instead, it should be a field annotated with #Autowired (or #Resource).

Spring AnnotationHandlerMapping not working

I'm new to spring controllers using annotated controllers.
Here is my configuration
Bean definition
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
Controller
package learn.web.controller.annotation;
import javax.servlet.http.HttpServletRequest;
import learn.web.controller.BaseController;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class FirstController extends BaseController {
#RequestMapping("/annotation/first.ftl")
public ModelAndView first(HttpServletRequest request) {
if(messageSource instanceof ReloadableResourceBundleMessageSource){
ReloadableResourceBundleMessageSource m = (ReloadableResourceBundleMessageSource) messageSource;
m.clearCache();
}
messageSource.getMessage("learn.message.first", new Object[] {},
localResolver.resolveLocale(request));
return new ModelAndView("/annotation/first");
}
}
When tried to access the given URL Spring is throwing a warning org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Learn/annotation/first.ftl] in DispatcherServlet with name 'springapp'
I think what you are missing is the component scan
<context:component-scan base-package="learn.web.controller" />
Add this to your configuration and try.
This will load all annotated components from the specified package
Your configuration may look like this
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="learn.web.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
</beans>

Resources