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
4
votes
3 answers

Mock a ConstraintValidator of a @Validated annotated controller method on Spring

Using Spring Boot 1.3.6.RELEASE, i am trying to unit test a controller method using Junit, Mockito and MockMvc. I have built a custom constraint validator (extending ConstraintValidator) which autowires a service. My target entity is annotated with…
alextsil
  • 504
  • 3
  • 11
  • 26
4
votes
1 answer

What is the dependency I need to get jsonpath matching to work with Spring mock mvc?

I’m using Hamcrest 2.0.0.0 and trying to get json-path matching to work using Spring (v 3.2.11.RELEASE)’s mockmvc framework. I have this in my JUnit (v 4.11) test mockMvc.perform(get("/api/users/" + id)) .andExpect(status().isOk()) …
Dave
  • 15,639
  • 133
  • 442
  • 830
4
votes
1 answer

jsonpath: No value for JSON path: $.id, exception: Path 'id' is being applied to an array. Arrays can not have attributes

i tried to read the content of the json with jsonPath and i get an error. Here the junit test method: mockMvc.perform(get("/path") .andExpect(status().isOk()) .andExpect(jsonPath("$.id", is(1))) …
emoleumassi
  • 4,881
  • 13
  • 67
  • 93
4
votes
1 answer

HttpRequestMethodNotSupportedException: Request method 'POST' not supported

Creating a unit test with MockMvc I am running into: HttpRequestMethodNotSupportedException: Request method 'POST' not supported Which causes the test case to fail expecting a '200' but getting a '405'. Some of the additional things you may see in…
mstelz
  • 610
  • 4
  • 9
  • 19
4
votes
1 answer

Mock controller with an object parameter

I'm trying to test a method with this signature: @RequestMapping(value="/Employee/{id}", method=RequestMethod.PUT, consumes="application/json") @Transactional public @ResponseBody Map update(@PathVariable Integer id, @RequestBody…
Alex2330
  • 347
  • 1
  • 7
  • 21
4
votes
2 answers

MockMvc WebAppConfiguration: Load servlet mappings in web.xml

I'm writing integration tests using MockMvc, and I'm wondering if there's a way to load servlet mappings from web.xml (which normally shouldn't matter). I have a custom HandlerInteceptor that matches the request URI (from HttpServletRequest) against…
nebulabrot
  • 465
  • 1
  • 6
  • 13
3
votes
1 answer

Can't Autowired MockMvc using @WebMvcTest

I'm trying to test my REST Controller in my Spring Boot application. This is my test class import com.binar.kelompok3.secondhand.repository.OffersRepository; import org.springframework.beans.factory.annotation.Autowired; import…
BrownFox
  • 103
  • 8
3
votes
0 answers

How to test a BadRequest exception on a POST request

I made a test that checked whether a post method from my controller does what it's supposed to. It worked great! Now I'm supposed to make a test to see whether the right message pops up when I get an 400 error for that post method. Here's what I've…
3
votes
0 answers

@AuthenticationPrincipal is always null when testing Spring Security

I'm writing a test for my view controller and use MockMVC. Every method in controller uses @AuthenticationPrincipal annotation for getting the user. I tried to mock user with @WithMockUser() and with SecurityMockMvcRequestPocessors. And it works…
3
votes
2 answers

org.springframework.web.util.NestedServletException : Request processing failed For MockMVC in unit test

When I tried to write negative unit test case for getting UnauthenticatedException which is user defined exception, After executing mockmvc code it is throwing me an exception back and test case is…
3
votes
0 answers

MockMVC Resets Spring Security Context after each call

I have a custom annotation with a custom filter to authorize incoming requests. The issue is that when I try to test my controllers with MockMvc the Spring Security Context is reset after each use of MockMvc therefore I can only run tests where I…
Archmede
  • 1,592
  • 2
  • 20
  • 37
3
votes
1 answer

Stackoverflow error while using MockitoMVC with Junit in Spring App

I am trying to test my Spring Application which consists of JWT auth with Junit and Mockmvc. Signup test is working as expected. But when trying to test login, StackOverflowError is displayed. Inside the JWTAuthenticationFilter.java, data is being…
Ujjwal Mishra
  • 177
  • 1
  • 8
3
votes
2 answers

MockMvc POST returning 400 - expecting 201

I'm trying to write a controller unit test for a @PostMapping but am getting a failed test Status expected:<201> but was:<400> The controller works as expected in Postman so I know it actually works, but it would be nice to have a working unit test…
3
votes
2 answers

Is it possible to add an assertion message when using MockMvc?

Most of the times instead of adding comments in an ordinary JUnit assertion, we add a message to the assertion, to explain why this is assertion is where it is: Person p1 = new Person("Bob"); Person p2 = new Person("Bob"); assertEquals(p1, p2,…
George Z.
  • 6,643
  • 4
  • 27
  • 47
3
votes
2 answers

MockMvc: Cannot resolve print method in andDo(print())

I'm new to JUnit testing. I'm now trying to test a Spring endpoint using MockMvc, but the andDo(print()) method can not be found. Are there any things I have to import in order to use this or what? @Autowired private MockMvc mockMvc; @Test …
Tsao
  • 139
  • 1
  • 10