Questions tagged [spring-test-mvc]

spring-test-mvc based on spring-test provides a unit testing framework for spring-mvc services.

spring-test-mvc based on provides a unit testing framework for services.

spring-test-mvc might be included in the Spring Framework version 3.2.x

Spring One DC 2012 slides

356 questions
5
votes
3 answers

Spring Controller Testing. Can't seem to find MockMvcRequestBuilders

I'm trying to write some Unit and Integration tests for my Spring Controllers following this guide and Spring's documentation for testing MVC controllers The problem is that I'm unable to find the appropriate includes in mvnrepository for the…
user6123723
  • 10,546
  • 18
  • 67
  • 109
5
votes
3 answers

Testing Spring controllers using Powermock

I have a class which tests specific controller and it works fine @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml") @TestExecutionListeners({ …
Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
4
votes
1 answer

@WebMvcTest using security returns 401 status instead of redirection

I'm writing a test for a simple controller with Spring Security. There is a login form enabled. When user enters /books URL they are redirected to login page. And this is what I see in web console. GET on /books returns 302 followed by /login and…
mate00
  • 2,727
  • 5
  • 26
  • 34
4
votes
1 answer

@Autowire MockMvc - Spring Data Rest

Given the Repository public interface ResourceRepository extends CrudRepository { ... } The following test code: @WebMvcTest @RunWith(SpringRunner.class) public class RestResourceTests { @Autowired private MockMvc mockMvc; …
dardo
  • 4,870
  • 4
  • 35
  • 52
4
votes
1 answer

Spring MVC test Mulitple file upload

I have controller, which handles multiple file upload: @PostMapping("/import") public void import(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) { assertUploadFilesNotEmpty(files); ... } And I want to test it …
xLatency
  • 365
  • 4
  • 12
4
votes
1 answer

Spring using MockMvc Test with CORS filter

I have am trying to run a basic MVC test @Test public void shouldReturnDefaultMessage() throws Exception { this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()) .andExpect(content().string(containsString("Hello…
isADon
  • 3,433
  • 11
  • 35
  • 49
4
votes
2 answers

H2 in memory is empty when I run my unitary tests but it´s populated when I run the application itself

I am somewhat new to Spring Boot and for learning purposes, I wrote a simple REST API using an in-memory database (H2). Everything works fine when I actually run the application: the database is loaded into memory and populated using an import.sql…
StatelessDev
  • 294
  • 4
  • 15
4
votes
1 answer

NullPointerException when using springBootTest annotation with WebEnvironment

I am using spring-boot 1.4.0.RELEASE. Here is my test class @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment= WebEnvironment.RANDOM_PORT) @WebAppConfiguration public class ApplicationTests { @Test public void contextLoads() { …
brain storm
  • 30,124
  • 69
  • 225
  • 393
4
votes
1 answer

spring-boot mvc tests: how to disable validation?

i have controllers with @Valid annotation. by default, mockMvc tests triggers that validation. i would like to disable this for some tests because it requires much more work to prepare valid input, while only partial input is enough for most tests
piotrek
  • 13,982
  • 13
  • 79
  • 165
4
votes
1 answer

Spring Security Unit Test - MockMvc perform test with custom user

I'm setting up unit tests for my Spring MVC Controllers and I am attempting to leverage the Spring MVC test framework. For each endpoint in my controller, I want to ensure that only users with designated authorities have access. My problem is that…
4
votes
1 answer

Spring load testing

I've implemented a testing unit using spring (mock mvc), and I'm looking for a tool to run this unit in many threads/processes (so it will act as load testing for my server). I've seen applications like the grinder and jmeter but I don't want to…
user_s
  • 1,058
  • 2
  • 12
  • 35
4
votes
2 answers

unit test Spring MissingServletRequestParameterException JSON response

I have POST method in a Spring boot rest controller as follows @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map bookmarkPost( @RequestParam(value="actionType",required=true)…
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
4
votes
0 answers

Spring MVC Test with Tiles 3 in IntelliJ doesn't work

I have the following very simple Test: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @Transactional @ContextConfiguration(SpringTestContext.WEB_APPLICATION) public class SpringWebContextTest { @Autowired private…
4
votes
3 answers

Spring MVC Test Framework - Unsupported Media Type

I am writing a test for controller using spring test mvc framework. I am trying to test a post request and it produces JSON. The actual code is running but test is failing. I an getting the error Status expected 200 but getting 415 which means…
vineet
  • 269
  • 1
  • 4
  • 18