Questions tagged [spring-mvc-test]
241 questions
157
votes
26 answers
How to avoid the "Circular view path" exception with Spring MVC test
I have the following code in one of my controllers:
@Controller
@RequestMapping("/preference")
public class PreferenceController {
@RequestMapping(method = RequestMethod.GET, produces = "text/html")
public String preference() {
…

balteo
- 23,602
- 63
- 219
- 412
118
votes
3 answers
Mock MVC - Add Request Parameter to test
I am using spring 3.2 mock mvc to test my controller.My code is
@Autowired
private Client client;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String initUserSearchForm(ModelMap modelMap) {
User…

jackyesind
- 3,343
- 14
- 47
- 74
76
votes
6 answers
Testing Spring MVC @ExceptionHandler method with Spring MVC Test
I have the following simple controller to catch any unexpected exceptions:
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(Throwable.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
…

C0deAttack
- 24,419
- 18
- 73
- 81
50
votes
8 answers
Failed to load ApplicationContext for JUnit test of Spring controller
I want to write a test case to check my controller (getPersons). This is a server side code. I have confusion what should i put inside @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-contest.xml"})
Secondly, I'm getting some…

Saurabh
- 955
- 6
- 14
- 22
44
votes
3 answers
Isolated Controller Test can't instantiate Pageable
I have a Spring MVC Controller which uses Pagination Support of Spring-Data:
@Controller
public class ModelController {
private static final int DEFAULT_PAGE_SIZE = 50;
@RequestMapping(value = "/models", method = RequestMethod.GET)
…

Jens Schauder
- 77,657
- 34
- 181
- 348
34
votes
3 answers
Unit testing controllers with CSRF protection enabled in Spring security
Recently we have introduced CSRF protection for our project which uses spring security 3.2.
After enabling CSRF some of the unit tests are failing because of the csrf token is not present in request. I put some dummy value into '_csrf' parameter and…

uiroshan
- 5,021
- 2
- 39
- 37
19
votes
2 answers
Unit tests passing through Maven, but failing through Cobertura: "Expecting a stackmap frame at branch target 65"
I recently added the Cobertura plugin to my Java/Spring-MVC project. The strange thing is that all my unit tests were passing, and they still pass when Maven does its initial test run, but then when Cobertura tries to run the tests, they all fail…

UpAllNight
- 1,312
- 3
- 18
- 30
17
votes
4 answers
Setting up MockMvc with @WebMvcTest in Spring Boot 1.4 MVC Testing
I have few working code to set up MockMVc in different ways with the new Spring Boot 1.4 @WebMvcTest. I understand the standaloneSetup approach. What I want to know is the difference between setting up MockMvc through WebApplicationContext and by…

user2693135
- 1,206
- 5
- 21
- 44
17
votes
6 answers
Assertion error: No value for JSON Path in JUnit test
I have written a test and it succeeded before but now I get an AssertionError: No value for JSON Path.
@Test
public void testCreate() throws Exception {
Wine wine = new Wine();
wine.setName("Bordeaux");
…

Robin van Aalst
- 217
- 1
- 2
- 7
17
votes
4 answers
@WebAppConfiguration not injected
I am trying to create spring-mvc tests using Spring 3.2.1. Following some tutorials, I thought this would be straight-forward.
Here is my test:
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( loader =…

sonoerin
- 5,015
- 23
- 75
- 132
16
votes
1 answer
Test HTTP status code of redirected URL with MockMvc
I want to test the login process in a Spring Boot Application using MockMvc. After the successful login, the user gets redirected to /home. To test this, I use:
@Test
public void testLogin() throws Exception {
RequestBuilder requestBuilder =…

Ben
- 1,579
- 4
- 20
- 34
16
votes
2 answers
How to test DeferredResult timeoutResult
I'm implementing long polling as per the Spring blog from some time ago.
Here my converted method with same response signature as before, but instead of responding immediately, it now uses long polling:
private Map

Tim
- 19,793
- 8
- 70
- 95
14
votes
1 answer
Spring MVC Controller Exception Test
I have the following code
@RequestMapping(value = "admin/category/edit/{id}",method = RequestMethod.GET)
public String editForm(Model model,@PathVariable Long id) throws NotFoundException{
Category category=categoryService.findOne(id);
…

Agung Setiawan
- 1,134
- 3
- 13
- 32
13
votes
1 answer
Spring Boot integration tests: @AutoConfigureMockMvc and context caching
I'm building very basic web application using Spring Boot 1.5.1 and wanted to create integration tests for checking REST endpoints.
As recomended by documentation, MockMvc might be used for it.
Here is very simple test class:
package…

Vitaljok
- 594
- 1
- 6
- 13
12
votes
2 answers
@ExtendWith(SpringExtension.class) not working
I'm trying to use Junit 5 on a spring-boot 2.x project to test a Controller.
The following works fine
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static…

Anand Rockzz
- 6,072
- 5
- 64
- 71