Questions tagged [spring-mvc-test]

241 questions
2
votes
0 answers

Testing Rest Controller

I want to test Rest Controller but not sure how to do it. Right now, I have a setup method, in which I delete all records from table ITEMS and input some records on which I will be testing rest controller methods. Is it a good idea? Or should I…
cerbin
  • 1,180
  • 15
  • 31
2
votes
0 answers

During Spring MVC Test getting error "Status expected:<200> but was:<400>"?

Please help me out in solving the error. I want to test my Rest Controller using MockMvc but after testing i am getting the above error. My GetTest is getting successfully run but my POST test is throwing out error. Maybe the way I have written the…
2
votes
2 answers

Unit tests - How to use Spring MockMVC to call soap service

I am unit testing my rest service using the code listed below and the request is successfully hitting the service. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static…
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
2
votes
1 answer

Matcher for JSONPath expression to find order of fields

I have an API that returns a JSON object that will be displayed in a UI. The order of the fields in the object matters because the UI wants to display the fields in that order. I want to write a unit test to ensure the order of the fields in the…
thrillstone
  • 31
  • 1
  • 4
2
votes
1 answer

Spring MVC Testing config

I'm getting into SPring MVC Test framework and cannot make it works. When i try to start test, get this: java.lang.IllegalStateException: Failed to load ApplicationContext at…
Unknwn Artist
  • 430
  • 5
  • 17
2
votes
0 answers

Integration test for restful service using Spring MockMvc (ignoring IDs or other fields in json content response)

I've created working integration test for my application: @Test public void postCalendar() throws Exception { User actualUser = createUser(); actualUser = userRepository.save(actualUser); String externalId = randomUUID().toString(); …
Sergii
  • 7,044
  • 14
  • 58
  • 116
2
votes
2 answers

How to test Spring MVC controller with pagination?

I am facing a problem to test a controller with pagination for my Spring boot MVC web project which uses Thymeleaf . My controller is as follows: @RequestMapping(value = "admin/addList", method = RequestMethod.GET) public String druglist(Model…
mnhmilu
  • 2,327
  • 1
  • 30
  • 50
2
votes
3 answers

Custom RequestMappingHandlerMapping with MockMvc

I have a custom RequestMappingHandlerMapping class that interprets a special annotation as part of its mapping criteria. It is being instantiated as a bean thusly: @Configuration @EnableWebMvc public class ConfigServletConfig extends…
2
votes
1 answer

Reuse Spring Mockmvc in unit tests

Can Mockmvc be reused within a test suite? I have several test suites so having to initialize the mockmvc between every test and then running all tests is really slow! I've tried putting mockMvc into the annotation @BeforeClass rather than in…
diepjy
  • 283
  • 2
  • 10
2
votes
1 answer

Spring MVC for Rest: How set a Locale value

I am working with Spring Framework 4.3.2 About Rest for POST and PUT the following situation happens. For POST I have the following: @PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}) public…
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
2
votes
1 answer

How can I use a Spring HttpRequest in my controller?

I've set up my test this way @SpringBootTest @AutoConfigureMockMvc @RunWith( SpringRunner.class ) public class PublicControllerTest { @Autowired private MockMvc mvc; this is my controller signature @GetMapping( produces =…
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
2
votes
1 answer

Testing model attribute in Spring Boot Controllers

I'm new to unit testing. I'm trying to make tests to a Spring Boot application controller. But the test cannot find my model attribute or something like that. Below you can find my code and hopefully help me discover what I am doing wrong. Thanks in…
Mateus M.R.
  • 86
  • 1
  • 9
2
votes
0 answers

unit test mockUser

How can I use @WithMockUser annotation in my unit test and then retrieve this user into my controller under test? @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) …
Claudio Pomo
  • 2,392
  • 7
  • 42
  • 71
2
votes
1 answer

Spring MVC Test, MockMVC: Conveniently convert objects to/from JSON

I am used to JAX-RS and would like to have similar comfort when sending requests using Spring MVC and working with the responses, i.e. on the client side inside my tests. On the server (controller) side I'm quite happy with the automatic conversion,…
C-Otto
  • 5,615
  • 3
  • 29
  • 62
2
votes
1 answer

SpringBoot junit tests with multiple TomcatServletContainerInitializer 's - BindException: Address already in use"

I have spring boot app and currently all my test are running green if ai run them separately i.e. manually, but when i run the maven package command then all test run in a serial mode i.e. after each other. What happens then is that i…
Tito
  • 2,234
  • 6
  • 31
  • 65