Questions tagged [spring-mvc-test]
241 questions
2
votes
2 answers
Spring MockMvc second test fails when using StreamingResponseBody
I have a simple Spring Boot service that uses StreamingResponseBody to write the response.
When testing this with MockMvc, the first test gives me the correct response, but the second one has an empty response body.
However, when I add a little…

Rob Gansevles
- 143
- 1
- 10
2
votes
1 answer
Spring Boot @WebMvcTest with constructor injection not working
Constructor injection with @WebMvcTest is NOT working. The mocked bean SomeService is not initialized. Why?
Doesn't Mockito create SomeService independently of Spring Boot?
If I use @MockBean everything is ok but I'd like to make use of constructor…

enhancedJack
- 265
- 1
- 6
- 15
2
votes
0 answers
How to enable Async in springboot testing using webmvctest?
This my test case:
@Test
public void sampleTestCase(int parm1, int parm2, Integer expectedReturnedMessageCode, String expectedReturnedparm2, Integer expectedReturnedparm1) throws Exception {
MvcResult result = mockMvc
…

Saed Manaf
- 41
- 5
2
votes
2 answers
How to generate exception in Spring MVC test from MockMultipartFile?
I am trying to write some unit tests for a controller in Spring MVC, and part of the controller method has the follwing code:
try {
newProjectFile.setFileType(fileType);
…

M Hall
- 51
- 5
2
votes
2 answers
Spring MVC test case
Am new to Spring MVC, i have written web servise using spring MVC and resteasy. My controller is working fine, now need to write testcase but i tried writtig but i never succed am also getting problem in autowiring.…

thamaiyanthi
- 21
- 1
- 3
2
votes
1 answer
spring mock MVC ,getting the right error message now MethodArgumentNotValidExceptionis thrown
my phrases is to check my controller, how does it serialize exceptions, while I am using javax.validation( JSR 380 )
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =…

yoav.str
- 1,547
- 6
- 33
- 73
2
votes
1 answer
writing negative unit test cases for controller classes :springboot
Is there a way i could write some negative test cases for the controller class
@RestController
@RequestMapping(value = "/health")
@Api(value = "EVerify Health check API", description = "Health check API")
public class HealthStatusController {
…

wandermonk
- 6,856
- 6
- 43
- 93
2
votes
1 answer
Snapshot testing in Spring MVC test
Is there any existing solution for doing snapshot testing [1] in Spring MVC test (MockMvc)?
Something like:
this.mockMvc.perform(get("/users")
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
…

Matija Folnovic
- 906
- 2
- 10
- 22
2
votes
1 answer
is that ok to have @Autowire or @Inject in @Configuration class
Spring 5, Java 8
I have multiple configuration files, one of the configuration file has @Autowire dependency. it does not complain on run time and works fine but intellij warns can't find those beans.
wondering if thats ok to have @Autowire or…

d-man
- 57,473
- 85
- 212
- 296
2
votes
1 answer
JpaRepository findById method returning Optional.empty when id is matching to the correct database row?
For some reason my PluginRepository interface doesn't want to return the object saved in my database in this test here.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class…

StackDrenny
- 165
- 1
- 2
- 11
2
votes
0 answers
@MockMvc does not work with validation annotated with @Valid
None of these solutions helped here: Spring mockMvc doesn't consider validation in my test. Added all of these dependencies, nothing helps.
I use Spring Boot 2.1.2, pom.xml:
org.springframework.boot
…

FreeOnGoo
- 868
- 1
- 8
- 26
2
votes
2 answers
Spring MVC RestController test failed to find valid mapping
I failed to test RestController method with path variable
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my/{env}] onto handler 'myController'
INFO…

Ori Marko
- 56,308
- 23
- 131
- 233
2
votes
0 answers
How can I test the Hystrix-Fallback with JUnit MockMVC
I need help with the jUnit testing, my result is json, and I need to test the Fallback method, that I bacome the RuntimeException that I build in the Update method in the Service. The test dont go to the fallback.
Controller.java
private static…

emibo34
- 113
- 1
- 1
- 3
2
votes
0 answers
Spring Security - Integration Tests with user
I have an integration test for Spring (with Spring Security) that looks like this:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RootControllerIT {
@LocalServerPort
…

mikeb
- 10,578
- 7
- 62
- 120
2
votes
1 answer
How can I mock service in ResponseEntityExceptionHandler @ControllerAdvice
I have Controller:
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService= userService;
}
…

ip696
- 6,574
- 12
- 65
- 128