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
12
votes
6 answers

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

I am trying to get my feet wet with TDD. I am trying to write unit test cases for controllers using Mockito in conjunction with MockMvc and Junit. But I am getting a runtime error thereby failing my test. At first I was facing problem in…
Sourabh
  • 1,253
  • 6
  • 21
  • 39
11
votes
4 answers

MockMVC is not autowired, it is null

I am having an issue with injecting MockMvc to my test class. I tried a few options, but none of them works. 1 option: basically the same as here, manually creating MockMvc @Autowire MockMvc - Spring Data Rest 2 option: package…
bogdasha
  • 153
  • 1
  • 2
  • 11
11
votes
6 answers

MockMvc returning 404 status

I am trying to write test cases for controller. I do not want to mock out my service as I would like to use these tests as complete functionality tests. I am trying to test this controller: @Controller public class PlanController { @Autowired …
Gaurav Kumar Singh
  • 582
  • 2
  • 5
  • 14
11
votes
1 answer

Run unit tests on controllers that require authentication

I have a spring boot application which requires login for some actions. I am trying to test them using MockMvc, but it doesn't seem to work. I keep getting a HTTP response with status 403 (forbidden). Probably there is something wrong with the…
aochagavia
  • 5,887
  • 5
  • 34
  • 53
11
votes
3 answers

@ModelAttribute controller spring-mvc mocking

I want to test a controller which is using @ModelAttribute for one of its method arguments. public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity) @ModelAttribute method getExampleEntity is using…
Remik
  • 25
  • 1
  • 2
  • 11
11
votes
2 answers

spring 4.1.1, mockmvc and do not want url encoding of HTTP GET request

Using MockMVC in tests and I need to test a GET URL that is already URL encoded: http://host:port/app/controller/[ALREADY URL ENCODED] The code: mockmvc.perform(get("/controller/[ALREADY URL ENCODED]") However in the logs I see that the URL has…
Khanna111
  • 3,627
  • 1
  • 23
  • 25
11
votes
2 answers

I need to create methods get () and status () to create a test controller with mockmvc?

I am trying to test my first controller, followed a few examples on the internet, but is in error in methods get() and status() to compile. import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import…
Tiago Costa
  • 181
  • 1
  • 2
  • 4
10
votes
3 answers

SpringBoot @WebMvcTest security issue

I have a spring rest mvc controller which has the url "/public/rest/vehicle/get". In my security configuration, I have defined that any requests for /public/rest should not require authentication. http. csrf().disable() …
Imran
  • 1,732
  • 3
  • 21
  • 46
10
votes
1 answer

WebMvcTest in Spring Boot

I have a basic SpringBoot app., embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have this class: @Service public class I18NService { /** The application logger */ private static final Logger LOG =…
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
10
votes
1 answer

Check Map key/values with jsonPath

I'm testing a controller that returns a Map @RequestMapping("/") @ResponseBody public Map getMessages(@RequestBody String foo) { Map map = boo.getMap(foo); return map; } Test: ... resultActions …
xedo
  • 1,117
  • 3
  • 18
  • 34
10
votes
4 answers

Unit Testing /login in Spring MVC using MockMvc

I have a very simple REST application created using Spring MVC. (Code is available at GitHub.) It has a simple WebSecurityConfigurer as follows: @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity …
Volkan Yazıcı
  • 1,530
  • 1
  • 15
  • 28
10
votes
2 answers

java.lang.AssertionError: Content type not set - Spring Controller Junit Tests

I am trying to do some unit testing on my controllers. No matter what I do all controller tests return java.lang.AssertionError: Content type not set I am testing that the methods return json and xml data. Here is an example of the…
blong824
  • 3,920
  • 14
  • 54
  • 75
9
votes
2 answers

Spring security not calling my custom authentication filter when running JUnit tests

I'm trying to implement custom stateless authentication with Spring Security by following this article The problem I'm facing is that my custom filter is not being called by the framework, even when my SecurityConfig looks almost the same as in the…
franDayz
  • 883
  • 10
  • 22
8
votes
2 answers

Spring Controller Unit Test throws NestedServletException

I have a Spring controller that throws an error when there is no data. I want to test the exception, which is custom NoDataFoundException , but it always throws org.springframework.web.util.NestedServletException The unit test error message is:…
hades
  • 4,294
  • 9
  • 46
  • 71
8
votes
1 answer

How to compare two jsonPath values in MockMvc

I am writing tests for my spring application using MockMvc. Assume that my json result will have the following format: { "available": true, "location": [ {"ID": 1, "path": "local1"}, {"ID": 2, "path": "local2"}, {"ID": 3, "path":…
Ock
  • 1,262
  • 3
  • 18
  • 38
1 2
3
49 50