Questions tagged [spring-mvc-test]
241 questions
3
votes
1 answer
Springboot 2.7 : Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required in @WebMvcTest for spring boot with mybatis
Below is my PaymentController Unittest
@WebMvcTest(PaymentController.class)
public class PaymentControllerTest {
@MockBean
PaymentService paymentService;
@Autowired
MockMvc mockMvc;
@Test
public void…

OTUser
- 3,788
- 19
- 69
- 127
3
votes
3 answers
java.lang.ExceptionInInitializerError when trying to mock
I'm running into this issue when trying to mock an object of the interface org.springframework.ui.Model. I'm using Mockito 3.5.15 w/Java 8 and JUnit Jupiter. I've tried using both @Mock and Mockito.mock(), globally and inside a method, but get the…

vnavs
- 51
- 1
- 1
- 8
3
votes
2 answers
How to exclude @EnableJpaRepositories from test?
I have a main @SpringBootApplication which needs to scan a specific package in order to enable the JPA repositories, so I use @EnableJpaRepositories to specify that. Right now I'm implementing unit tests and I want to test the Controller component…

Erick Siordia
- 171
- 1
- 10
3
votes
0 answers
Spring MVC - MockHttpServletResponse Adds charset to Content-Type - How to disable that?
When testing my endpoint with MockMvc, the tests are failing because my endpoint returns content type application/json, but MockHttpServletResponse is adding the charset option, then the test fails saying that application/json;charset=UTF-8 was…

GreenSaguaro
- 2,968
- 2
- 22
- 41
3
votes
2 answers
Mockito when().thenReturn() Returning Null when it should return empty list
I've been trying to figure out why my mocked findIngredientsByCategory method is returning null when I have when(controller.findIngredientsByCategory(any()).thenReturn(Collections.emptyList()). This implementation works for the findAll method…

baron.jos
- 55
- 1
- 1
- 5
3
votes
0 answers
@WebMvcTest equivalents annotation for Spring MVC application
I am looking for a feature that is similar to what @WebMvcTest does.
What @WebMvcTest does .
1. only instantiating the web layer, not the whole context
2. Only works with Spring Boot application (as it is in the package -…

yas
- 486
- 1
- 8
- 23
3
votes
3 answers
Is there a way to include a spring component in a WebMvcTest
Given production code classes:
@RestController
@RequiredArgsConstructor
public class MyController {
private final MyValidator validator;
// annotations relating to request mapping excluded for brevity
public void test(@Valid…

David
- 7,652
- 21
- 60
- 98
3
votes
1 answer
Spring MVC custom Formatter works in Test but fails in Browser
I have a controller: (as per Spring WebMVC @ModelAttribute parameter-style)
@GetMapping("/time/{date}")
@ResponseStatus(OK)
public LocalDate getDate(
@ModelAttribute("date") LocalDate date
) {
return date;
}
LocalDateFormatter encodes…

Denis Babochenko
- 335
- 2
- 8
3
votes
2 answers
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required in spring mock mvc test for spring boot with mybatis
the demo project location
https://github.com/soliders/mockmvctest-ofspringboot-withmybatismapper
https://github.com/soliders/mockmvctest-ofspringboot-withmybatismapper.git
explain:
the demo use Spring Boot/…

Longshi Shen
- 41
- 1
- 3
3
votes
1 answer
Spring Boot Test with @WebMvcTest fails on Atlassian Bitbucket Pipeline
When running some @WebMvcTests locally, I do not have problems (Spring Boot 1.5.8, gradle 4.6):
@RunWith(SpringRunner.class)
@WebMvcTest(VZNFCController.class)
public class VZNFCControllerTest {
private VZNFCTagAction action1 = new…

thomi
- 1,603
- 1
- 24
- 31
3
votes
1 answer
How test correctly the LoginForm through @WithAnonymousUser
About Spring Security
For the class that extends WebSecurityConfigurerAdapter I have the following for the configure method:
.and()
.formLogin()
.loginPage("/perfom/login")
…

Manuel Jordan
- 15,253
- 21
- 95
- 158
3
votes
0 answers
Spring MVC Testing with mixed calls through MockMvc and a Service using @WithMockUser fails - SecurityContext is cleared by MockMvc
I get An Authentication object was not found in the SecurityContext error when I call a @Service after calling a @RestController through MockMvc in a test method annotated with @WithMockUser.
Calling in the opposite order works fine as does…

thewaterwalker
- 322
- 2
- 11
3
votes
0 answers
Test ExceptionHandling with MockMvc and Mockito
I want to test my RestController with MockMvc and Mockito. I have providerController with injected service and providerService throw ProviderNotFoundException if prover with this id not found. Also i have ExceptionHandler for this…

eugene-karanda
- 760
- 1
- 6
- 9
3
votes
1 answer
How to access a CSRF token in a Spring controller unit test?
I have a Spring controller that upon an authenticated GET from "/user" returns the following JSON:
{"name":,"token":}
I tried to construct a unit test for the controller that will verify that the returned JSON contains a…

user1408140
- 639
- 3
- 9
- 20
3
votes
1 answer
Using Spring security test to test a secured Spring MVC controller
following the documentation about using Spring Security Test to write tests for a spring MVC app that is wired behind Spring Security.
This is a vanilla spring-boot application employing a typical spring-security wiring. Here's the main…

apil.tamang
- 2,545
- 7
- 29
- 40