spring-mvc-test

Empty content in spring mvc test

Deadly 提交于 2021-02-08 13:20:24
问题 I could not test page content with spring mvc test because it is empty. Given simplest possible controller: @RequestMapping(value = "/home") public String home(HttpSession session, ModelMap model) { return "home"; } relevant tiles config: <definition name="base.definition" template="/jsp/view/application.jsp"> <put-attribute name="header" value="/jsp/view/header.jsp" /> <put-attribute name="menu" value="/jsp/view/menu.jsp" /> <put-attribute name="title" value="" /> <put-attribute name="body"

UriComponentsBuilder/MvcComponentsBuilder usage in a Spring Boot Test

橙三吉。 提交于 2021-01-29 09:21:38
问题 I've put a very simple sample project on GitHub to reproduce the problem. The main issue is that I have a PersonController that has a PutMapping to create a new person. In order to populate the Location header with the URL to fetch that person, I add the UriComponentsBuilder as parameter for that PutMapping , as you can see here: @PostMapping public ResponseEntity<Person> add(@RequestBody final PersonForCreate personForCreate, UriComponentsBuilder uriComponentsBuilder) { Person newPerson =

Spring MVC Test - multipart POST json originalFilename is required

一世执手 提交于 2020-01-06 14:15:46
问题 I use spring-boot 2.0.3.RELEASE with junit5. I have just tried to test multipart request with mockMvc. MockMultipartFile file = new MockMultipartFile("file", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, "<<pdf data>>".getBytes(StandardCharsets.UTF_8)); MockMultipartFile metadata = new MockMultipartFile("metadata", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(metadata).getBytes(StandardCharsets.UTF_8)); this.mockMvc.perform(multipart("/documents") .file(file) .file

How to junit return type of a method in spring mvc controller

☆樱花仙子☆ 提交于 2020-01-03 04:07:26
问题 I am doing junit on my Spring MVC controller - @RequestMapping(value = "index", method = RequestMethod.GET) public HashMap<String, String> handleRequest() { HashMap<String, String> model = new HashMap<String, String>(); String name = "Hello World"; model.put("greeting", name); return model; } And below is my junit for the above method - public class ControllerTest { private MockMvc mockMvc; @Before public void setup() throws Exception { this.mockMvc = standaloneSetup(new Controller()).build()

Spring MVC testing (security Integration test), JSESSIONID is not present

雨燕双飞 提交于 2020-01-02 02:38:29
问题 I have created custom login form for my spring boot app. In my form integration test, I want to check that received cookies contain JSESSIONID and XSRF-TOKEN . But, I received only XSRF-TOKEN . Here is my test: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest("server.port:0") public class UserIT { @Autowired private WebApplicationContext context; @Autowired private FilterChainProxy

@WebAppConfiguration not injected

…衆ロ難τιáo~ 提交于 2020-01-01 04:06:47
问题 I am trying to create spring-mvc tests using Spring 3.2.1. Following some tutorials, I thought this would be straight-forward. Here is my test: @RunWith( SpringJUnit4ClassRunner.class ) @ContextConfiguration( loader = AnnotationConfigContextLoader.class, classes = { JpaTestConfig.class } ) @WebAppConfiguration public class HomeControllerTest { @Resource private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Test public void testRoot() throws Exception { mockMvc.perform

How to check JSON response in Spring MVC test

一个人想着一个人 提交于 2019-12-24 19:40:07
问题 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; @PostConstruct void init() { servlet = new MyResource(); beanFac.autowireBean(servlet); } @RequestMapping(value = "/servlet/api/update", method = RequestMethod.POST) public MyResponse handle(HttpServletRequest request, HttpServletResponse response, @RequestBody String json) {

JPA foreign key is null in the child table

百般思念 提交于 2019-12-24 18:40:18
问题 I have below classes i am trying to get onetomany relationship using JPA. When the xml message sent to restcontroller, the message is stored both parent and child table but foreign key is always null. Please let me know if any issue. Rest Service which i am using to persist the data and the below is the sample xml which i am posting to this rest method. @RequestMapping(value="/team/", method = RequestMethod.POST , headers="Accept=application/xml") @ResponseStatus(value = HttpStatus.OK) public

Spring autowire HttpServletRequest in integration tests

£可爱£侵袭症+ 提交于 2019-12-24 16:40:42
问题 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 obtained in @Configuration: @Configuration class Config { @Autowire @Bean @Scope("request") MyObject provideMyObject(HttpServletRequest req) { return req.getAttribute("myObj"); } } Everything works well in main code, but not in testing: when I run it from an

Spring security DefaultMethodSecurityExpressionHandler bean is not registered for Integration Test's default spring security config

余生长醉 提交于 2019-12-24 02:56:20
问题 I am attempting to write Spring MVC integration test with Spring Security and Thymeleaf for the view layer. I have setup my MockMvc object with Spring Security Integration just like all the examples from the documentation. Integration Test setUp: import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security