spring-mvc-test

Spring MVC Test with RequestPostProcessor vs. Annotations

[亡魂溺海] 提交于 2019-12-05 19:54:40
I have an application I've created with JHipster. I generated a Blog entity, then modified the BlogResource class so its getAll() method only returns the blog for the current user. /** * GET /blogs -> get all the blogs. */ @RequestMapping(value = "/blogs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @Timed public List<Blog> getAll() { log.debug("REST request to get all Blogs"); return blogRepository.findAllForCurrentUser(); } BlogRepository has the following for its findAllForCurrentUser() method. @Query("select blog from Blog blog where blog.user.login = ?#

Testing Spring MVC Router with MockMVC

ぐ巨炮叔叔 提交于 2019-12-05 02:51:32
I'm trying to test my Spring MVC webapp with Spring test. It uses springmvc-router for routing and that appears to break the tests, which work fine when I use @RequestMapping instead of my routes.conf file. I have a .jsp file called valid.jsp , and it displays fine when I run the development site from Jetty. The controller is: @Controller @EnableWebMvc public class AuthController { public String valid() { return "valid"; } } My routes.conf file maps GET /valid authController.valid . Now, my tester looks like @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/test

Unit tests passing through Maven, but failing through Cobertura: “Expecting a stackmap frame at branch target 65”

99封情书 提交于 2019-12-04 15:52:19
问题 I recently added the Cobertura plugin to my Java/Spring-MVC project. The strange thing is that all my unit tests were passing, and they still pass when Maven does its initial test run, but then when Cobertura tries to run the tests, they all fail with the same error message: Expecting a stackmap frame at branch target 65 in method xxx.xxxx.xxxx.xxxx;)V at offset 40 I have no idea why this is happening and don't even know how to go about fixing it. I've searched the internet but haven't found

@WebAppConfiguration not injected

吃可爱长大的小学妹 提交于 2019-12-03 10:28:58
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(get("/").accept(MediaType.TEXT_PLAIN)).andDo(print()) // print the request/response in the console

Failed to load ApplicationContext for JUnit test of Spring controller

半腔热情 提交于 2019-12-03 10:23:08
I want to write a test case to check my controller (getPersons). This is a server side code. I have confusion what should i put inside @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-contest.xml"}) Secondly, I'm getting some errors like this: Failed to load application context. Can not find the path [which I specified in @ContextConfiguration] I have a structure like this: restAPI *src/main/java com.company.controller personController.java *Test com.company.testController personControllerTest.java *src main webapp WEBINF app-context.xml @Autowired private PersonService

Unit tests passing through Maven, but failing through Cobertura: “Expecting a stackmap frame at branch target 65”

∥☆過路亽.° 提交于 2019-12-03 09:59:14
I recently added the Cobertura plugin to my Java/Spring-MVC project. The strange thing is that all my unit tests were passing, and they still pass when Maven does its initial test run, but then when Cobertura tries to run the tests, they all fail with the same error message: Expecting a stackmap frame at branch target 65 in method xxx.xxxx.xxxx.xxxx;)V at offset 40 I have no idea why this is happening and don't even know how to go about fixing it. I've searched the internet but haven't found any similar problems. I use JUnit and spring-test-mvc for testing. Has anyone seen this before? Of

Spring Boot 2, Spring Security 5 and @WithMockUser

眉间皱痕 提交于 2019-12-01 23:15:50
Since I migrated to Spring Boot 2.0.5 from 1.x, with no mean to disable security, I can't get test roles to work on mock MVC tests : @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class ApplicationsControllerShould { ... @Autowired private MockMvc mockMvc; private ObjectMapper mapper = new ObjectMapper(); @Test @WithMockUser(roles = "ADMIN") public void handle_CRUD_for_applications() throws Exception { Application app = Application.builder() .code(APP_CODE).name(APP_NAME) .build(); mockMvc.perform(post("/applications") .accept(MediaType.APPLICATION_JSON_UTF8)

Spring Boot 2, Spring Security 5 and @WithMockUser

。_饼干妹妹 提交于 2019-12-01 22:31:10
问题 Since I migrated to Spring Boot 2.0.5 from 1.x, with no mean to disable security, I can't get test roles to work on mock MVC tests : @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class ApplicationsControllerShould { ... @Autowired private MockMvc mockMvc; private ObjectMapper mapper = new ObjectMapper(); @Test @WithMockUser(roles = "ADMIN") public void handle_CRUD_for_applications() throws Exception { Application app = Application.builder() .code(APP_CODE).name(APP

Empty Exception Body in Spring MVC Test

这一生的挚爱 提交于 2019-12-01 15:50:18
I am having trouble while trying to make MockMvc to include the exception message in the response body. I have a controller as follows: @RequestMapping("/user/new") public AbstractResponse create(@Valid NewUserParameters params, BindingResult bindingResult) { if (bindingResult.hasErrors()) throw BadRequestException.of(bindingResult); // ... } where BadRequestException looks sth like this: @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "bad request") public class BadRequestException extends IllegalArgumentException { public BadRequestException(String cause) { super(cause); } public

Empty Exception Body in Spring MVC Test

别说谁变了你拦得住时间么 提交于 2019-12-01 15:33:25
问题 I am having trouble while trying to make MockMvc to include the exception message in the response body. I have a controller as follows: @RequestMapping("/user/new") public AbstractResponse create(@Valid NewUserParameters params, BindingResult bindingResult) { if (bindingResult.hasErrors()) throw BadRequestException.of(bindingResult); // ... } where BadRequestException looks sth like this: @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "bad request") public class BadRequestException