I am trying to Unit Test a Spring MVC Controller. But don’t know how to do it. So can anyone tell me how to Unit Test a Spring Web MVC Controller using JMockit
Use Spring MockMvc to write unit test cases for controller.
Refer https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/web/servlet/MockMvc.html
For all I know, MockMvc is just testing the Controller, and mocking the Service layer.
Whilst RestAssured and TestRestTemplate are testing the running instance of our API.
Is that correct?
And what's the difference between RestAssured and Spring Boot's TestRestTemplate?
MockMvc is one of the classes in spring-test. This is primarily used for unit testing of the controller layer. Not just your controller class. This is for tetsing the controller layer. But you have to mock service and other layers. Hence it is primarily used for unit testing.
TestRestTemplate is again part of spring test, as the documentation says,
Convenient alternative of {#link RestTemplate} that is suitable for
integration tests.
This can be used to test your Rest Service/ endpoints. One of the main difference is you use MockMvc for unit testing and TestRestTemplate for Integration testing. In other words, for using MockMvc, you don't need a running instance of server, but for TestRestTemplate you would need.
RestAssured is a completely different framework. This has nothing to do with Spring. This is a librariy, which provides various ways to test any REST service with fluent BDD style interface.
As mentioned MockMvc is used to mock the service layer. It is useful in unit-testing of the code.
Whereas both RestAssured and TestRestTemplate are used for integration-testing which is end to end APIs testing.
Also, there is not much difference between RestAssured and Spring Boot's TestRestTemplate. You can use RestAssured for Spring-Boot Application or can go ahead with TestRestTemplate which is a Spring library.
MockMvc is primarily used for web layer testing. Web layer testing is essentially writing fine-grained tests specifically designed to test your app’s controllers. It is very similar to writing regular unit tests for classes where you need mock dependencies for testing specific methods.
As far as comparing RestAssured vs TestRestTemplate they do pretty much the same thing. When it comes to RESTful based API integration testing and validation, TestRestTemplate and RestAssured both offer convenient methods to create and execute your HTTP calls with custom headers, auth, content types, query parameters, payload content, cookies, etc. The main difference -aside from syntax- is that TestRestTemplate is part of Spring’s test framework which comes bundled with the spring-boot-starter-test dependency.
Check out this article - Testing Spring Boot RESTful APIs using MockMvc/Mockito, Test RestTemplate and RestAssured - it has additional explanation and robust examples on the usage for all three (MockMvc, TestRestTemplate, and RestAssured).
I am trying to see if i can automate a spring boot batch processing built with JDBC template and i have never worked on it. i automate web based projects using cucumber and selenium web driver. Is it even possible? help please.
Thanks
If I understand you correctly, it is possible to write automated tests for spring boot batch application using cucumber.
This is how I have it implemented -
Dependencies - cucumber-java, cucumber-junit, cucumber-spring
gherkin features like usual
cucumber test cases - use SpringApplication.run(YourApplication.class) to run the Spring Boot application, an use SpringApplication.exit() - to verify exit status codes for Batch as needed.
Here is an article which I found helpful for integrating cucumber with Spring Boot - http://www.baeldung.com/cucumber-spring-integration
we have a spring boot application which we want to test via serenity (former Thucydides). Theoretically the tests can be run (if i test for example www.google.com everything works fine) but I want to test my own application and not google ;)
So I need to start the application before running the tests. Normally we have an annotation
#RunWith(SpringJUnit4ClassRunner.class)
at our test class. but with Serenity and cucumber we need
#RunWith(CucumberWithSerenity.class)
and it is not possible to add 2 #RunWith annotations.
What is the best way to get the tests wit Serenity and Cucumber running?
Upgrade to Spring 4.2.1 and you should be able to use the Serenity runner:
http://docs.spring.io/spring/docs/4.2.1.RELEASE/spring-framework-reference/htmlsingle/#testcontext-junit4-rules
I'm testing a Spring web MVC application with heavy RESTful invocation that POST and DELETE a remote resource on demands.
When I'm trying to run integrated test, obviously I need to test POST first and then DELETE second. Unfortunately JUnit doesn't support such dependency test, and the vanilla JExample class cannot be run with Spring application context. Is there a feasible way of using both?