Questions tagged [spring-mvc-test]

241 questions
12
votes
3 answers

Empty Exception Body in Spring MVC Test

I am having trouble while trying to make MockMvc to include the exception message in the response body. I have a controller as follows: @RequestMapping("/user/new") public AbstractResponse create(@Valid NewUserParameters params, BindingResult…
Volkan Yazıcı
  • 1,530
  • 1
  • 15
  • 28
11
votes
2 answers

Mocking a Spring Validator when unit testing Controller

While writing unit tests postmortem to code that another project created, I came across this issue of how to mock a validator that is bound to the controller with initBinder? Normally I would just consider making sure my inputs are valid and be done…
t0mppa
  • 3,983
  • 5
  • 37
  • 48
10
votes
2 answers

Interrogation about Spring MVC test API's model().attribute() method

I am trying to test the following controller method using the Spring MVC test API: @RequestMapping(value = "/preference/email", method = RequestMethod.GET, produces = "text/html") public String emailForm(@ModelAttribute EmailInfo emailInfo, Model…
balteo
  • 23,602
  • 63
  • 219
  • 412
10
votes
2 answers

Spring MVC test with MockMvc

I'm trying to run a test for testing a Spring MVC controller. The test compile and runs, but my problem is that I got a PageNotFound warning: WARN PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name '' My…
jorgen
  • 1,217
  • 6
  • 22
  • 41
9
votes
1 answer

How can I use @WebMvcTest for a Controller that uses an autowired ConversionService?

In a Spring Boot application, I have two POJOs, Foo and Bar, and a BarToFooConverter, which looks like: @Component public class BarToFooConverter implements Converter { @Override public Foo convert(Bar bar) { return new…
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
9
votes
1 answer

Testing Spring MVC Router with MockMVC

I'm trying to test my Spring MVC webapp with Spring test. It uses springmvc-router for routing and that appears to break the tests, which work fine when I use @RequestMapping instead of my routes.conf file. I have a .jsp file called valid.jsp, and…
AAA
  • 1,364
  • 1
  • 10
  • 15
7
votes
3 answers

cors not working for my spring boot application

Here is my webConfig @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } } I have a /health…
brain storm
  • 30,124
  • 69
  • 225
  • 393
7
votes
2 answers

Spring MVC testing (security Integration test), JSESSIONID is not present

I have created custom login form for my spring boot app. In my form integration test, I want to check that received cookies contain JSESSIONID and XSRF-TOKEN. But, I received only XSRF-TOKEN. Here is my…
Hutsul
  • 1,535
  • 4
  • 31
  • 51
7
votes
1 answer

Empty content in spring mvc test

I could not test page content with spring mvc test because it is empty. Given simplest possible controller: @RequestMapping(value = "/home") public String home(HttpSession session, ModelMap model) { return "home"; } relevant tiles…
takacsot
  • 1,727
  • 2
  • 19
  • 30
6
votes
1 answer

Spring Boot 2, Spring Security 5 and @WithMockUser

Since I migrated to Spring Boot 2.0.5 from 1.x, with no mean to disable security, I can't get test roles to work on mock MVC tests : @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class ApplicationsControllerShould { …
6
votes
1 answer

Disable spring boot starter Cassandra for unit testing

I need to use Cassandra in a project with the following gradle dependecy: compile "org.springframework.boot:spring-boot-starter-data-cassandra" The problem is that although Cassandra AutoConfiguration classes are marked to be excluded from a unit…
6
votes
2 answers

Spring MVC Test setup fails on loading ApplicationContext.xml

Currently I'm trying to setup Spring MVC Controller testing for a school project I'm working on next to my job. Normally I program in php and frameworks like Laravel so this is pretty new for me. The problem is that I can't figure out how to solve…
Lars Mertens
  • 1,389
  • 2
  • 15
  • 37
6
votes
1 answer

How to make unit test multipart with a PUT request using Spring MVC and Spock?

I have a controller like this one: @RestController @RequestMapping('/v1/document') class DocumentV1Controller { @PutMapping HttpEntity newdoc( @RequestHeader Map headers, @RequestParam('document') MultipartFile…
6
votes
2 answers

Setting request parts in spring test mvc

I'm trying to test (through Spring test (mvc)) a controller that uses servletRequest.getParts() All I've read so far is that MockMvcRequestBuilders.fileUpload().file() is the solution. However I cannot make it work. I wrote the following test which…
benjamin.d
  • 2,801
  • 3
  • 23
  • 35
6
votes
3 answers

Spring MockMvc and async controller's HTTP status code

How do i validate/test the 500 internal server error in MockMvc, when my controller is of Async servlet nature? I am writing unit test cases for my REST endpoint as part of a test cases i need to validate that the server sends 500 internal error as…
Naveen Kumar
  • 893
  • 11
  • 19
1
2
3
16 17