I have a Spring service I want to test that uses the #Validated annotation on one of its method parameters. I would like to test it in the Spring container, but I am not sure if that is the best way. If testing in the container is the best solution for my situation, I would like to know how to run it in the container without loading my complete configuration. Any thoughts?
I ended up using the Spring Test Framework. I am using the standalone and the web application context of the mock MVC to test my annotations.
Spring 3.2.4 Test Framework
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html
Configuration Example
http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-configuration/
Related
I have a common project I'd like to import to both a spring boot application and a J2EE application. One of the Objects is a set of attributes set from parameters. In spring boot I'd like to use #Value to set them, for the non-spring platform I have to load the properties with code. I'd like to use the same object for both environments since it's use is pervasive. My attempts to use builders have failed because they inevitably get instantiated before the spring objects.
Who's done this?
Thanks in advance
I'd hoped for something more elegant but maybe I'm spoiled by Spring Boot. I had to replace the #Autowired parameters class with a builder that implements ApplicationContextAware. The setApplicationContext method will instantiate the parameters object if it gets executed (the builder class was loaded by Spring and is aware) or by the simple retrieval of parameters for a non-spring environment.
What's the best method to initiate a new Spring Project ?
Initiating Spring project is a lot of pain with various xml and db configuration.
Is there an "official" repository with complete sample project, as MVC with db access and so ?
Spring Boot may also be the solution, but some point still not clear to me :
How to add external components (such as Quartz) without creating some xml config ? (No such xml in Boot apparently)
Is a Spring Boot builded app is production-proof ?
As writen in the comments http://start.spring.io/ is the best way to start Spring boot project(STS has integration for it).
If you want to use something that is not supported by Spring Boot you can init spring beans the same way you do it in xml, just use java configuration. See this for example: http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm
Also useing xml is still available. You can add #ImportResource on your Configuration class
#EnableAutoConfiguration
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})
I am currently experimenting with the support for WebSockets added in Spring 4.0, as described in this guide. As demonstrated in the guide, methods annotated with #MessageMapping can be added to any Spring MVC controller, which may also contain #RequestMapping methods.
The spring-test module has support for writing integration tests for #RequestMapping methods (as described here) in a very simple and fluid way:
#Test
public void getAccount() throws Exception {
this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.name").value("Lee"));
}
Is there similar support for testing #MessageMapping methods using WebSockets? I have not found anything in any of the Spring modules, and none of the WebSocket guides contain any tests. If not, would I need to actually deploy the application and use a WebSocketConnectionManager to connect a test client? Or is there some API I can build on from spring-test?
This sample project contains such a small test client, but I would prefer to integrate this into the actual tests without requiring me to deploy the application and hardcode the deployed path in the tests.
There is nothing like Spring MVC Test for #MessageMapping methods yet. However, a similar approach to testing should be possible even without the fluent API. There is a ticket in JIRA (see https://jira.spring.io/browse/SPR-11266) to provide documentation so watch that ticket for more details in the very near future.
I am trying to run some performance test cases on an spring application using jmeter.
I know that in jmeter, spring annotation would not work, like for example
#ContextConfiguration and #Autowired
which I over came by getting the ApplicationContext and then getting the Bean.
But, is there any other work around because I have a very big Base class which has lot of
#Autowired instance variables and lot of dependencies.
Thanks in Advance.
I'm not familiar with JMeter but I think you can treat your JMeter test cases as a standalone Spring application in which you load the ApplicationContext once then all your Spring Beans will be available.
See this tread: Using Spring 3 autowire in a standalone Java application
Hope this helps.
How can I call a spring annotated controller from a JUnit test, in a way so that spring comes into play with binding and all, but without making a http request (just mocking out the request object)? It has to include the whole shebang from the controller and down, with JPA and database and all. We are also using EJB, so maybe a bean can do this for me?
The reason for this, is that I would like to have some automatic tests that tests performance of specific controller calls, but without the client and network traffic.
Thank you
There's a section in the Spring reference about Unit-testing Spring MVC.
Here's the relevant excerpt:
Unit testing Spring MVC Controllers
To test your Spring MVC Controllers, use
ModelAndViewAssert combined with
MockHttpServletRequest,
MockHttpSession, and so on from the
org.springframework.mock.web package.
Reference:
org.springframework.mock.web
package summary
ModelAndViewAssert javadoc
You can specify a test-specific spring context for your unit test like this:
#ContextConfiguration(locations = "classpath:spring/ITestAssembly.xml")
You can then make this context use a mock or embedded data source instead of the real database.