MVC Controller test and spring-data-jpa EnableSpringDataWebSupport - spring

I'm using spring-data-jpa #EnableSpringDataWebSupport and DomainClassConverter to not manually lookup the instances via the repository. When a do a controller test (MockMvc standalone setup test) on a controller like
#RequestMapping(value = '/user/{userId}', method = RequestMethod.GET)
public UserDetails detail(#PathVariable('userId') User user) {
...
}
I get a ConversionNotSupportedException. Is it possible to test controllers like this? What should I do?

I dont know it would be an option, but in my case I used to use HttpClient to test my controllers with IntegrationTest
HttpClient httpClient = login(HTTP_SERVER_DOMAIN, "user1#gmail.com", "password");
GetMethod getAllAdvicesMethod = new GetMethod(adviceGetURL);
getAllAdvicesMethod
.addRequestHeader("Content-Type", "application/json");
try {
httpClient.executeMethod(getAllAdvicesMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use Rest Template for Spring as well https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

Related

How to Method#getAnnotatedParameterTypes() in spring proxied class

I'm using spring-boot 2+ and created some custom annotation;
#Target({ElementType.PARAMETER})
#Retention(RetentionPolicy.RUNTIME)
public #interface MyCustomAnnotation{
}
When doing:
final AnnotatedType[] annotatedTypes = mostSpecificMethod.getAnnotatedParameterTypes();
//this will get the original class
//final Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
Class<?> annotatedMappedClass = null;
for (AnnotatedType annotatedType : annotatedTypes) {
if (annotatedType.isAnnotationPresent(MyCustomAnnotation.class)) {
annotatedMappedClass = TypeFactory.rawClass(annotatedType.getType());
}
}
it works when bean is not a proxy but when I add the #Transactional annotation it becomes a proxy and stops working. What is the Spring Util to find in the target class?
As far as I understood you'll need the bean. Using:
Method invocableMethod = AopUtils.selectInvocableMethod(mostSpecificMethod, bean.getClass());
seems to work.
Also a more complex one:
Method method = mostSpecificMethod;
if (AopUtils.isAopProxy(bean)) {
try {
Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
method = clazz.getMethod(mostSpecificMethod.getName(), mostSpecificMethod.getParameterTypes());
}
catch (SecurityException ex) {
ReflectionUtils.handleReflectionException(ex);
}
catch (NoSuchMethodException ex) {
throw new IllegalStateException("...", ex);
}
}

How to configure the ObjectMapper for Unirest in a spring boot project

I am using Unirest in a project which is working fine for me. However, I want to post some data and do not want to escape all the JSON as it looks ugly and is just a pain in the neck.
I found a few links on how to configure the ObjectMapper for Unirest and it gives this code.
Unirest.setObjectMapper(new ObjectMapper() {
com.fasterxml.jackson.databind.ObjectMapper mapper =
new com.fasterxml.jackson.databind.ObjectMapper();
public String writeValue(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(String value, Class<T> valueType) {
try {
return mapper.readValue(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
But, no examples show where it is best to do this in a Spring Boot API project.
I tried to set it up in the main class method, but I am getting an error that 'setObjectMapper' cannot be resolved. I also tried to do this in the controller but I get the same error.
My Gradle deps for these two libraries are:
// https://mvnrepository.com/artifact/com.mashape.unirest/unirest-java
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.5'
compile 'com.fasterxml.jackson.core:jackson-databind:2.10.1'
Can anyone show me how to use the Jackson object mapper with Unirest in a Spring Boot API project as I have been googling and reading docs for two days now. Would appreciate some help.
Thank you in advance
You have several issues here:
The version of unirest you're using (1.4.5) does not contain the feature to configure object mapper. This feature was added later (github PR). So you should update to the latest version available at maven central - 1.4.9. This alone will fix your compilation problem.
You can keep your Unirest configuration code in the main method. However if you want to use not default jackson ObjectMapper(), but the one from the spring context, then it's better to create something like a fake spring bean to inject ObjectMapper:
#Configuration
public class UnirestConfig {
#Autowired
private com.fasterxml.jackson.databind.ObjectMapper mapper;
#PostConstruct
public void postConstruct() {
Unirest.setObjectMapper(new ObjectMapper() {
public String writeValue(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(String value, Class<T> valueType) {
try {
return mapper.readValue(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}
Other than that it looks this library changed the package name. Now it's com.konghq. You might want to consider updating, but library API might have changed.
Upd: for the latest version
compile group: 'com.konghq', name: 'unirest-java', version: '3.1.04'
the new API is Unirest.config().setObjectMapper(...)

Spring #Transactional behavior calling both Transactional and Non-Transactional Methods

I'm looking at some existing code and wanted to know what happen's in the following scenario with Spring's #Transactional annotation? Consider the following example:
A POST request hits a #Controller annotated with #Transactional:
#ResponseBody
#Transactional
#RequestMapping(value="/send", method=RequestMethod.POST)
public void send(#RequestBody Response response) {
try {
DBItem updatedDbItem = repository.updateResponse(response);
if (updatedDbItem == null){
//some logging
}
} catch(Exception ex) {
//some logging
}
}
The controller calls a non #transactional repository method which sets a value and in turns calls a another #Transactional method:
#Override
public DBItem updateResponse(Response response) {
try {
DBItem dBItem = findResponseById(response.getKey());
if (dBItem != null){
dBItem.setSomeField(response.getValue());
return updateDataBaseItem(response);
}
} catch (Exception ex) {
//some logging
}
return null;
}
The following updateDataBaseItem() method is common and called from other non transactional methods as well as the above method:
#Transactional
#Override
public DBItem updateDataBaseItem(Response response){
try {
DBItem dBItem = em.merge(response);
return dBItem;
} catch (Exception ex) {
//some logging
}
return null;
}
send() => spring detect #transaction with default parameters
actually Propagation setting is REQUIRED and the spring join the exist transaction or create new if none.
repository.updateResponse(..) => No transactions params the method execute within the same transaction already exist
updateDataBaseItem(..) => calling the method in same repository , spring will not recognize the #Transaction annotation because the use of proxy mode, so this method will be executed within the same transaction
a method within the target object calling another method of the target
object, will not lead to an actual transaction at runtime even if the
invoked method is marked with #Transactional

Test case using SpringJunitRunner

I want to write junit test case for the below code with springJunitRunner.
the below piece of code is one service in a class.
#Component
#Path(/techStack)
public class TechStackResource {
#Autowired
private transient TechStackService techStackService;
#GET
#Path("/{id}")
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getTechStackById(final #PathParam("id") Integer technicalstackid) {
final TechStackResponse response = new TechStackResponse();
int statusCode = Constants.HTTP_STATUS_OK_200;
try {
TechStackModel techStackModel = techStackService.findObjectById(technicalstackid);
response.setGetTechStackDetails(GetTechStackDetails.newBuilder().technicalStack(techStackModel).build());
if (techStackModel == null) {
statusCode = Constants.HTTP_STATUS_ERROR_404;
}
} catch (EmptyResultDataAccessException erde) {
} catch (Exception e) {
LOGGER.error("Exception occured in TechStackResource.getTechStackById(technicalstackid) ", e);
throw new APMRestException(
"Exception while executing TechStackResource.getTechStackById(technicalstackid) ",
Constants.UNKNOW_ERROR, e);
}
return Response.status(statusCode).entity(response).build();
}
}
the configuration in web.xml for servlet is
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
Since you are using Jersey as well as Spring, you can use the SpringJunitRunner only to wire-up TechStackResource with its dependency TechStackService.
In order to test your REST handler method getTestStackById, you could go the POJO approach and invoke it directly. Alternatively, you can use Jersey's own MockWeb environment. To find out more about this, I recommend looking at the Jersey example sources, e.g. HelloWorld.

ResponseBody cannot be resolved to a type

I'm trying to write this method in my controller:
#ResponseBody
#RequestMapping(value = {"/getTeams"}, method = RequestMethod.GET)
public void getMaxRequestSize(HttpServletResponse response) {
String autoCompleteList = null;
List<Team> teams = atService.getAllTeams();
Iterator itr = teams.iterator();
while (itr.hasNext()) {
autoCompleteList += itr.next().toString() + "\n";
}
response.setContentType("text/html");
PrintWriter writer;
try {
writer = response.getWriter();
writer.write(autoCompleteList);
} catch (IOException e) {
e.printStackTrace();
}
}
For some reason I always get an error on the ResponseBody annotation (= cannot be resolved to a type). I googled for quite a while and didn't find a solution. I'm sure it's something silly. I can use all the other annotations without any problems...
Is this a Maven project? You might be ending up with the old Spring 2.5.6 jars in your war file instead of Spring 3. Eclipse's POM editor's Dependency Hierarchy tab can help you figure out if that's the case.

Resources