Is there a way to use AssertJ assertions with Spring MVC Test? - spring-mvc

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers.
But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC Test.
Below is an example using the Hamcrest API.
mockMvc
.perform(get("/user?operation=userList"))
.andExpect(status().isOk())
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList))
.andExpect(view().name(UserController.VIEW_USER_LIST))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasSize(2)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));

Update
If you would like to vote for inclusion of support for AssertJ assertions with MockMvc, please see the related Spring JIRA issue: SPR-16637.
Generally speaking, you may choose whatever assertion framework you like when testing with Spring.
However, the particular scenario you are describing involves the API of the Spring MVC Test framework. The methods in question are designed to be used with the Hamcrest Matcher API. It is therefore not possible to use AssertJ within those method calls.
Regards,
Sam (author of the Spring TestContext Framework)

I've put together a library that offers AssertJ assertions for MockMvc but also for ResponseEntity (returned by TestRestTemplate): https://github.com/ngeor/yak4j-spring-test-utils

There has recently been an issue raised on the Spring Boot project to discuss adding support for AssertJ assertions with MockMvc, it might be worth keeping an eye on it. You can view the issue here: https://github.com/spring-projects/spring-boot/issues/5729
It looks like the initial concept created by Phil Webb involves wrapping the MockMvc to provide support for AssertJ assertions.

Related

Vaadin 22 with Spring MVC and Spring Security

I am trying to get a basic Vaadin22 app running with Spring MVC and Spring Security. The vaadin docs seem to allude that this is possible,
If an application is based on Spring Boot, this mechanism can be enabled by using a set of annotations and with minimum Spring Security configurations. However, it is also possible to use it in Vaadin Spring applications that are not based on Spring Boot, with some extra configurations.
I have started with skeleton-starter-flow v22, forked to here and added basic Spring integration (see here). I have then attempted to add Spring Security.
I initially extended Vaadin's VaadinWebSecurityConfigurerAdapter (see here), but when I run the app it fails because there are missing autowired dependencies.
Next, I forced some component scanning to detect the missing dependencies (see here), but now there is a ClassNotFoundException because there is a dependency on the Spring Boot class, ServletRegistrationBean
Finally, I abandoned Vaadin's VaadinWebSecurityConfigurerAdapter and instead extended Spring's WebSecurityConfigurerAdapter (see here). Now the app runs, but I am not seeing the default login screen that Spring is supposed to provide by default.
Any assistance greatly appreciated.
Note: The app can be run using,
mvn jetty:run
So it seems that Vaadin's VaadinWebSecurityConfigurerAdapter is intended for use with Spring Boot (which I'm trying to avoid). In order to get basic Spring Security working, I extended Spring's WebSecurityConfigurerAdapter instead, but I was missing an implementation of Spring's AbstractSecurityWebApplicationInitializer which registers the appropriate security filter. See here for the working solution.
The problem with this, is that I will not get Vaadin's View-Based Access Control. In order to get this, I will need to start using/migrating functionality from VaadinWebSecurityConfigurerAdapter.

Are interceptors in Struts 2 the same as interceptors in Java Spring 3?

I am new to Web Development. I am learning Java Spring. I found a good online tutorial for interceptors in Struts 2. My question is, are interceptors in Struts 2 the same as interceptors in Java Spring? If not, what are the differences? If they are different, what are the advantages/disadvantages of using one over the other?
I would suggest that this is not a differentiator in choosing between Spring MVC ( if that's what you mean by Java Spring ) and Struts2. The interceptors in both frameworks play the same architecural role by isolating cross cutting concerns from your core business logic classes.
At least in Struts 2, it's fairly uncommon to code against the interceptor api; <5% of your total development time. This would suggest that you would only really care about the differences in the interceptor api's if you had some particular use case that you felt would be solved by interceptors, and then this use case could help you evaluate the two api's.
If you don't have such a use case, which seems likely if you are new to web app development, I recommend not putting to much into this topic; either framework will most likely server you well. Though there could be other valid reasons for choosing one or the other.

Ant and XML-based Spring security integration (without using annotations)

I am newbie to Spring .I built a spring MVC application which is XML based and I used JAR files using Spring Framework MVC application step-by-step.
Now I have to integrate spring security in it. I found many tutorials for spring security, but all are made using annotations. And in the MVC tutorial it uses Ant and XML. I am just lost in this part. Should I have to convert my whole application using annotations or what to do?
Is there any way using spring security without annotations? May be this question is not logical. But guide me and help me solve my confusion.
Or is there any tutorial help me in this regard, please let me know. Thank you
I'm not sure what you mean with "change my application to annotation base", but you will have to add some anotations above your methods like this:
#Secured("isAuthenticated() and hasRole('PERMISSION_BUY_ITEMS')")
public void buyItem(int itemId, int userId) {
// you buy stuff login
}
This way in order to access your method the user should be logged in (authenticated) and have the permission BUY_ITEMS.
You don't have to do this for all your methods.
If you do not need permission/role based authorization you can just use authentication based expressions like isAuthenticated(), isAnonymous() and permitAll out of the box without any custom implementation.

Spring MVC Portlet DefaultAnnotationHandlerMapping - Choosing Controller Class

I am trying to use DefaultAnnotationHandlerMapping for annotated Controllers in Spring Portlet MVC.
My portlet works with one controller. But I am not able to understand as to how DefaultAnnotationHandlerMapping is able to determine which Controller to choose among many controllers in the same package.
This link says that it searches for best matching Controller. But I could not understand the rules to determine the best matching controller.
Any clues?
Beware of the bug in Spring 3.1.2. I explained some portion of what you asked in Is this a Bug in Spring 3.1.2 ( specifically Spring Portlet MVC's )?
I also logged at bug https://jira.springsource.org/browse/SPR-9697 .
Hope you find it useful.

Can some one explain IResolver<T> internal interface in asp.net MVC 3?

The code in its derivatives MultiServiceResolver and SingleServiceResolver are highly compact and can somebody explain what they are doing or their purposes are and the role played by them in dependency resolution?
They're the ones responsible of going to the DependencyResolver and ask it to resolve the types they asks for (i/e IViewEngine or IControllerFactory).
more info about the DependencyResolver can be found here:
http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html
Basically SingleServiceResolver calls GetSingleService from the dependencyResolver, and MultiServiceResolver calls GetServices. You can set your own DependencyResolver to use your own IOC container (AutoFac/Ninject/StructureMap etc.) using the SetResolver method in the DependencyResolver static class. by default (If you don't override it) the resolver doesn't resolve any types.
Basically this decouples the implementations of some types (i/e ViewEngines/Collection or ValueProviderFactories/ValueProviderFactoryCollections) that use the ServiceLocator pattern in the ASP.NET MVC pipeline from the ASP.NET MVC implementation and the DependencyResolver static class.
Recently, I've created a blog post that a little explains MultiServiceResolver and SingleServiceResolver.
http://www.beletsky.net/2011/08/inside-aspnet-mvc-iresolver-and-its.html

Resources