Questions tagged [spring-mvc-test]
241 questions
2
votes
1 answer
Spring autowire HttpServletRequest in integration tests
We have singleton controllers like
@Controller
class C {
@Autowire MyObject obj;
public void doGet() {
// do something with obj
}
}
MyObject is created in a filter/interceptor and is put into HttpServletRequest attributes. Then it's…

Dzmitry Lazerka
- 1,809
- 2
- 21
- 37
1
vote
0 answers
How to register a ContraintValidator with MockMvc?
I know this was asked before, but that was never really answered satisfactory.
How do you add a ContstraintValidator with MockMvc?
I expected to find something on the StandaloneMockMvcBuilder, as I was able to use it to add an argument resolver, but…

Sled
- 18,541
- 27
- 119
- 168
1
vote
2 answers
How to create a general MockMvc class for multiple test classes?
I am writing end to end tests for many different Spring Controllers. Right now, I am trying to write a general class for testing that contains MockMvc performing methods. I have endpoints that I need to call in different Controllers and I do not…

M. Merim
- 45
- 9
1
vote
1 answer
Spring Boot Controller Test with Mock Service doesn't use the mock service
I have a simple RestController with a Service and want to test just the controller by providing a mock implementation for the Service.
However, I'm getting an empty response in resultActions = mockMvc.perform(get("/user")); object when I'm running…

VishalM93
- 33
- 5
1
vote
1 answer
Spring mvc test case with string in request header and multipart file as a request parameter for a post request
@Controller
@PostMapping("/hello/{studentName}")
public ResponseEntity method1(
@RequestMapping(value = "/upload/{studentName}", method = RequestMethod.POST)
@ResponseBody
public String saveAuto(
…

Anu Shree
- 11
- 1
1
vote
1 answer
WebMvcTest of @Controller with @RequestParam and status is not 200, but 400
i try to write @WebMvcTest:
@ExtendWith(SpringExtension.class)
@WebMvcTest(AudienceController.class)
class LectureControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private LectureService lectureService;
@MockBean
…

Jun K
- 37
- 1
- 6
1
vote
2 answers
how to use junit and mockito when we have a mulit layer service call approach in a restfull web service
i am using spring tool suite to write a code .there are 4 layers restContoller,buisnesslogic,domain ,service....
i want to test for a method of business logic layer where it calls a method of dao which finally calls a method of service layer to…

manish_mehta1996
- 11
- 1
1
vote
1 answer
Set authentication for Integration test of secured Controller
I cannot set authentication for my integration test of rest controller. Controller's method looks like this:
@RestController
@RequestMapping(BASE_URL)
@RequiredArgsConstructor
public class EventController {
public static final String BASE_URL =…

Denis Stephanov
- 4,563
- 24
- 78
- 174
1
vote
1 answer
Spring MVC - Error 404 with @EnableGlobalMethodSecurity when testing Controller using @WebMvcTest
We are trying to add unit testing to our Spring project Controllers (BTW, the Integration tests work fine), but we are suffering a very odd behavior, When we add the Configuration with @EnableGlobalMethodSecurity (with JSR-250 annotations) if the…

Roberto
- 8,586
- 3
- 42
- 53
1
vote
2 answers
Spring Request Mapping Regex "Not Contains" not working
I want my application to route to "/" whenever browser go to any path
except "/api".
here's what i did on my redirectIfNotApi() method in my controller.
@Controller
@RequestMapping("/")
class MyController {
@RequestMapping(value =…

tuty_fruity
- 523
- 1
- 5
- 18
1
vote
0 answers
Spring Boot 2 | Defining @RequestMapping in interfaces
I have created a simple spring-boot-starter-web(v2.1.5) project that needs to implement "controller" classes for the @RequestMapping interfaces generated by OpenApi/Swagger
This is how the generated interface looks:-
@Validated
@Api(value =…

Kumar Sambhav
- 7,503
- 15
- 63
- 86
1
vote
0 answers
Issue testing controller with spring mvc test (code status 404)
I am working with spring rest docs now and I am facing some issues to test my controller, basically I am getting a 404 issue:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /v1/browse/players/
Parameters =…

rasilvap
- 1,771
- 3
- 31
- 70
1
vote
2 answers
Java Spring MVC application - How to resolve this warning?
I have tried the solutions which were discussed in other discussions but nothing worked for me. Any help is appreciated
Here is my Web.xml file.

Ashish Shah
- 1,047
- 7
- 17
1
vote
1 answer
Async RestController endpoints :: Async result for handler was not set during the specified timeToWait=0
We have endpoints of the form:
@GetMapping("/myurl")
public Callable getARandomString(@RequestParam int a) {
The application is configured with just one thread (that's why we use Callables to handle this). However, in order to create the…

AntMor
- 417
- 6
- 18
1
vote
1 answer
How to check JSON response in Spring MVC test
I have a servlet defined in web.xml, so I defined it inside a Controller for testing only for MyResource:
@Controller
public class TestMyServlet {
MyResource servlet;
@Autowired
AutowireCapableBeanFactory beanFac;
…

Ori Marko
- 56,308
- 23
- 131
- 233