I was through the integration testing documentation for Grails and I noticed this line:
Grails does not invoke interceptors or servlet filters when calling actions during integration testing.
source: http://grails.org/doc/latest/guide/testing.html#integrationTesting
Why is this? It would make my testing life a lot easier if Grails did invoke the filters. My project makes heavy use of filters and many of my controllers depend on my filters in order to do anything.
I was thinking about it and it seems like one could use groovy black magic to automatically execute the filters in an integration test. Has anyone already done this, or is this something that I'd have to write?
The environment used for integration tests is similar to what's available during run-app; Spring is active, plugins are loaded, a database is available, etc. Pretty much everything except for a web server. Without a server, there are no real requests, no servlet filters, and no Grails filters (which are wrappers for Spring controller HandlerAdaptors). When testing controllers you can access a request and response thanks to the Spring servlet API mock classes. But none of the real web request lifecycle is active, it's all just simulated.
You're right that it should be doable with some custom code. When you do this, please consider making it a plugin so we can all share :)
Related
I am asked to choose one and the best from these two for unit and integration tests-
1. RestAssured
2. Mockmvc
The application is in Spring and Spring Boot. I have read many blogs and no clear distinction is given or I couldn't find one. For rest assured, its neat and clean code and BDD style that makes it more readable. But doesn't sound a convincing point. Some of the tests are written using Mockmvc and I am trying to write the same in RestAssured to compare the same.
I know this may sound a theoretical question but I am unable to get points in favor of one and suggest which one is better and why. Looks like a choice of flavor to me. Any views and suggestions?
You would choose MockMvc for web layer testing. These tests will allow you to validate your controller classes properly handle respective HTTP requests. Since these are practically fine grained controller unit tests, you can additionally include them as part of your apps’ code coverage percentage with tools like JaCoCo. On a side note, these tests will likely run faster than integration tests since they won’t require the web server to run on.
RestAssured is used for integration testing in your Spring Boot app. When it comes to RESTful based API integration testing and validation, RestAssured offers convenient methods to create and execute your HTTP calls with custom headers, auth, content types, query parameters, payload content, cookies, etc.
To aid your comparison check out this article - Testing Spring Boot RESTful APIs using MockMvc/Mockito, Test RestTemplate and RestAssured - it has a good explanation and robust examples on the usage for RestAssured and MockMvc.
We are using both Angular and Rails applications at our company. Is there a way to mock a web page to test the UI? Essentially I want to jump midway into an application, so I don't have to take time logging in, creating our object, tweaking the object, then finally getting to what I can test.
I was looking at something like MSL, but am unsure if it's really what I need.
Turns out MSL is what I wanted. My application depends on other applications for communication. In order to isolate my isolation, I can use MSL to mock the responses from and the requests to my dependent applications fairly easily. In my configuration I switch my external dependency to localhost:8001 (or whatever port msl is running on).
Edit
I rewrote MSL, specifically for my purposes and made it a little easier. The server is a node program, and the client is only in ruby as of now.
mobe-server
mobe-client
I'm working on a webapp running on Tomcat which using spring-data to connect to a neo4j graph in embedded mode.
I would like to use neo4j server instead of the embedded mode and I am looking for some help to be sure about how to do that.
Some of my application services are quite difficult and combine, in a single transaction, the result of several cypher requests in a dto sent back to the user.
First I thought that I have to create a server unmanaged extension and I think I should follow these following steps.
- Keep my webapp with springMVC and spring security to hold and secure users sessions.
- Regroup all my transactional services in a specific jar my-app.jar
- Use Jax-RS to add a REST access point on each of my service of my-app.jar
- use something like spring restTemplate from my spring controller to call services from my-app.jar
First question : is this way of doing things is the good way ?
Second question : I have many spring injection in my services layer. How can I keep them working (how can I add dependencies in the server extension ?
Then I discovered graphAware and I wonder if I should use it instead.
And finally I just read this post http://jexp.de/blog/2014/12/spring-data-neo4j-improving-remoting-performance/ and it seems that I should use
the SpringCypherRestGraphDatabase (as explain in the bold text at the end of the article).
Well, I'm a little bit lost and I would appreciate any help to use neo4j server instead the embedded mode for my application which contain some complexe transactions.
You have a number of options here and you are on the right track with your thinking.
Option 1:
If your use cases are business-logic-heavy, and your question suggests that they are, going the unmanaged extension route is one option.
Essentially, you can then combine the most performant Java API and Cypher (if you wish) to perform your use case. I wouldn't use SDN here by the way, so you have to do your mapping manually, but is there really any mapping? Maybe you just want to execute traversals / Cypher queries for each one of your use cases.
Each use case then exposes a simple REST API, which is consumed by your Spring-powered application running Spring MVC, Spring Security, and all that. You can use the RestTemplate from Spring in your app's Controllers.
To add a twist to all that, you can use the GraphAware Framework to develop the "unmanaged extension" using Spring MVC as well. That would be my preferred option, knowing nothing about your domain/app.
Option 2:
Use the new version of SDN (v4) as Michael suggests. This allows you to run your application with annotated domain objects, Spring MVC, Security, et al. Operations (CRUD and other) are automatically translated to Cypher and sent across the wire to Neo4j running in server mode (no extensions needed). Results are then marshalled back to Java objects.
We're about to release Milestone 1 of SDN v4. It shouldn't take more than a week. That said, it is still going to be a Milestone release, thus not ready for production. A GA release is expected in May (ish).
You can already try SDN v4 yourself. Clone this repo: https://github.com/spring-projects/spring-data-neo4j, make sure you're on the 4.0 branch, and do an mvn clean install on it. Here's a sample app, built using Angular JS and Spring Boot.
Please do get in touch with feedback / questions / problems (best by email info at graphaware dot com). Cheers!
I suggest you wait a bit until SDN4 Milestone 1 comes out (developed by GraphAware) this was written from scratch for Neo4j-Server.
I have done the backend part of a project in java (Maven) and use a Restful design.
The structur in java/(Maven) is:
1) domainlayer
2) datalayer
3) webservicelayer(client)
And the frontend part will be done in groovy/grails.
The only parts I'm doing to write in grails are: controllers and views.
The structur is:
1)Controllers
2) views
My question is how we can link them two parts together. Since Restful part is written in Maven and creates a war file.
How can I get frontend connected to the backend?
One of the really great features of grails is the GORM, so I'm not sure why you'd want to skip that part. Typically you'd only use a controller if you needed to get some data from a backend and then forward to a view. In your case you've already written the backend, so you could actually just do ajax pages that call the REST interface, and that might be all you need. If that's the case you hardly need grails; you can just put the static pages under your project's src/main/webapp folder and you're done. On the other hand, if you want to use grails for the GSP pages, you can make the controllers be do-nothing (so it's just specifying routes really) and have the view be GSP pages that make ajax calls. If you're going to do this be sure to check out jquery since it has really nice ajax support.
REST is talking HTTP so your Grails application will be the client of that. Although, it is a bit wasteful to have Grails in the middle, you can probably design your front-end in JS and do it all in the browser bypassing Grails all-together.
If you are bent on going the Grails way, you will need an HTTP Client (commons HTTP client is a pretty good one) and then on the receiving end you will be able to parse responses, this is where Grails will actually be useful. grails.converters has a nice method for you
JSON.parse(responseText)
It is entirely possible there is a better way of either parsing or doing client HTTP. For example, grails' functional-test plugin wraps HTTP library so you have a bit of a groovy feel for that communication. You might want to look at how it is done there. Not sure if anything better can be done with regards to JSON parsing.
HTH,
Alex.
I've inherited a Grails app which makes calls out to a web service, using javax.xml.ws* classes and I'm trying to find a way to mock the web service based on the WSDL for the integration tests. I realize that I can use one of the java soap implementations to build this, but I'd rather stay in groovy.
So my question is, is there an idiomatic groovy way to build a web service based on a WSDL?
One approach that is pretty straightforward is to use Jetty to create a mock. It´s easy to create a mock that looks at the request and generates a response, especially if you´re using Groovy. For instance, create a template response file and use the XmlSlurper to fill in values. Then you can either start the mock in your tests or run it independently.
I found the following blog that roughly explains the basic concept:
http://olafsblog.sysbsb.de/lightweight-testing-of-webservice-http-clients-with-junit-and-jetty/