Questions tagged [mockmvc]

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

742 questions
5
votes
1 answer

How do I make @WebMvcTest pickup WebMvcConfigurer classes from custom starters?

I'm building a custom starter library which registers an ObjectMapper in a WebMvcConfigurer class. One of the settings on this ObjectMapper is an Instant serialization format. When I use @SpringBootTest with @AutoConfigureMockMvc the configured…
5
votes
3 answers

MockMvc: changing default character encoding of MockHttpServletResponse from ISO-8859-1 to UTF-8

While writing Spring Itegration Tests I had the problem that MockMvc ignored my .accept(MediaType.APPLICATION_JSON_UTF8) setting, and returned ISO-8859-1 with bad looking umlaut. What is the best way to set default encoding of MockMvc to UTF-8?
R.A
  • 1,813
  • 21
  • 29
5
votes
0 answers

Configure MockMvc for WebMvcTest with spring-security in spring-boot 2.1.1

is there any known issue with running a WebMvcTest in Spring-Boot 2.1.1 together with MockMvc and Spring-Security 5.1.2? Because I cannot get it to work - but maybe you see where I missed something. Here is my setup with…
digital-h
  • 301
  • 3
  • 11
5
votes
5 answers

MockMvc seems to be clear SecurityContext after performing request (java.lang.IllegalArgumentException: Authentication object cannot be null)

I'm trying to run some integration test using SpringBoot + Spring Data Mongo + SpringMVC I've simplified and generified the code but it should be able to reproduce the behavior with the following test. As you can see from BookRepository interface I…
snovelli
  • 5,804
  • 2
  • 37
  • 50
5
votes
1 answer

Documenting null values with Spring REST Docs

Let's say we have the following API: @RestController public class PersonController { @GetMapping("/api/person") public List findPeople() { return Arrays.asList(new Person("Doe", "Foo", "John"), new Person("Doe", null,…
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
5
votes
1 answer

Body is empty when using MockMvc



@RunWith(SpringRunner.class) 
@WebMvcTest(CustomerController.class) 
public class CustomerControllerMvcTest {

    @Autowired  
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @MockBean   
    private…
Ashish Sharma
  • 617
  • 6
  • 15
5
votes
1 answer

How to use Gson instead of Jackson for MockMvc?

I have spent the past day trying to find a solution to this and could not find any online resource that solves this. I am using Gson for Message conversion for my application, which works fine outside of unit testing. I even added a…
Brandon
  • 404
  • 6
  • 21
5
votes
2 answers

@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404

Assertion error using @RequestMapping annotation outside of the class I am getting this error message: java.lang.AssertionError: Status Expected :200 Actual :404 My Controller is like this @Service @RestController …
Spyros_av
  • 854
  • 2
  • 8
  • 24
5
votes
1 answer

JsonPathRequestMatchers to ResultMatcher using MockMVC

I am testing a todo controller using MockMVC: mockMvc.perform(MockMvcRequestBuilders.get("/toDos/") .with(user("user").password("password").roles("ADMIN")) .content("{ \"saved_date\":…
Chayma Atallah
  • 725
  • 2
  • 13
  • 30
5
votes
1 answer

Unit Test Spring MVC Rest Service: array jsonPath

In my Spring controller I have a method that returns the following json: [ { "id": 2, "dto": null, "user": { "userId": 2, "firstName": "Some", "lastName": "Name", "age": 100, "aboutMe": "boring" }, …
user2983190
5
votes
0 answers

Flaky tests of async spring controller with MockMvc

I'm trying to test an async spring controller that returns a DeferredResult: MvcResult result = mvc.perform(get("/test")) .andExpect(request().asyncStarted()) .andReturn(); result =…
moen
  • 73
  • 5
5
votes
2 answers

WebApplicationContext does not Autowire when injecting in Spock Specification

Although I followed the Spring Boot Guide, when trying: @SpringApplicationConfiguration(classes=MainWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class) @WebAppConfiguration @ActiveProfiles("integration-test") class…
thomi
  • 1,603
  • 1
  • 24
  • 31
5
votes
1 answer

Authentication token passed to ControllerAdvice is null when running through MockMvc

I am using Spring Boot 1.3, Spring 4.2 and Spring Security 4.0. I am running integration tests using MockMvc, for example: mockMvc = webAppContextSetup(webApplicationContext).build(); MvcResult result = mockMvc.perform(get("/")) …
Martin Wilson
  • 3,386
  • 1
  • 24
  • 29
5
votes
1 answer

Setting HttpServletRequest as a testing method argument using MvcMock

I am in process of writing a junit for one of the controller methods with method signature as below: @RequestMapping(value="/getTokenizedURL.json",method=RequestMethod.POST) @ResponseBody public ResponseData getTokenizedURL(@RequestBody final…
k_b
  • 343
  • 3
  • 16
5
votes
1 answer

Getting Error for MockMvc perform() Methods

I am creating a Spring MVC Controller test. Compiler is showing errors for methods shown below in bold. Am I missing some library or something in my code? Any suggestions? I am using following dependencies:
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94