spring-test-mvc

Issue with @WithUserDetails and spring boot 1.4 TestEntityManager

二次信任 提交于 2019-12-01 05:03:09
问题 I have an issue with Spring Boot's TestEntityManager and @WithUserDetails annotation. Here is my test suite: public class AdvertisementAuthorizationTest extends AbstractIntegrationTest { private static final String IMPERSONATOR_EMAIL = "joe.hacker@gmail.com"; private final static String OWNER_OF_ADVERTISEMENT_EMAIL = "john.nice@gmail.com"; @Autowired private TestEntityManager testEntityManager; @Autowired private MockMvc mockMvc; private Advertisement advertisement; private UserAccount

Testing with spring-test-mvc jsonpath returns null

徘徊边缘 提交于 2019-12-01 00:02:02
I am using Spring's "spring-test-mvc" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have: @Test public void shouldGetAllUsersAsJson() throws Exception { mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON)) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("fName").exists()); } The above test returns: java.lang.AssertionError: No value for JSON path: fName To quickly check what I actually get I ran the below test: @Test public void shouldPrintResults() throws Exception { mockMvc.perform

MockMvc WebAppConfiguration: Load servlet mappings in web.xml

你说的曾经没有我的故事 提交于 2019-11-30 23:25:20
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 a template (using AntPathMatcher ). In web.xml, I define servlet mappings like this (with a corresponding mobile-context.xml): <servlet-mapping> <servlet-name>mobileServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> So when a controller defines a mapping like "/operation" , requests should really be made to "/services

How to unit test a secured controller which uses thymeleaf (without getting TemplateProcessingException)?

荒凉一梦 提交于 2019-11-30 20:33:42
I am trying to run a unit test in spring-boot using spring security and a simple home (root) controller which uses thymeleaf for the template processing. I am trying to write some unit tests to verify that my security permissions are working right and that the right data is hidden or shown from my template (which uses the thymeleaf spring security integration). The app itself does work correctly when I run it. I just want to verify it is working with a set of integration tests. You can find all the code here but I will include relevant snippets below also: https://github.com/azeckoski/lti

MockMvc WebAppConfiguration: Load servlet mappings in web.xml

≯℡__Kan透↙ 提交于 2019-11-30 18:32:24
问题 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 a template (using AntPathMatcher ). In web.xml, I define servlet mappings like this (with a corresponding mobile-context.xml): <servlet-mapping> <servlet-name>mobileServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>

How to check JSON in response body with mockMvc

拥有回忆 提交于 2019-11-30 16:50:29
This is my method inside my controller which is annotated by @Controller @RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8") @ResponseBody public JSONObject getServerAlertFilters(@PathVariable String serverName) { JSONObject json = new JSONObject(); List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, ""); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(filteredAlerts); json.put(SelfServiceConstants.DATA, jsonArray); return json; } I am expecting {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} as my

Use @WithMockUser (with @SpringBootTest) inside an oAuth2 Resource Server Application

混江龙づ霸主 提交于 2019-11-30 11:50:21
Environment : I have a spring boot based microservice architecture application consisting of multiple infrastructural services and resource services (containing the business logic). Authorization and authentication is handled by an oAuth2-Service managing the user entities and creating JWT tokens for the clients. To test a single microservice application in its entirety i tried to build tests with testNG , spring.boot.test , org.springframework.security.test ... @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, properties = {"spring.cloud.discovery.enabled=false", "spring

java.lang.AssertionError: Content type not set while junit Spring MVC Controller?

我是研究僧i 提交于 2019-11-30 08:39:06
问题 I am using JUnit to test my Spring MVC controller. Below is my method which returns a index.jsp page and shows Hello World on the screen - @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 test for the above method: public class ControllerTest { private MockMvc mockMvc; @Before

How to unit test a secured controller which uses thymeleaf (without getting TemplateProcessingException)?

痞子三分冷 提交于 2019-11-30 04:50:48
问题 I am trying to run a unit test in spring-boot using spring security and a simple home (root) controller which uses thymeleaf for the template processing. I am trying to write some unit tests to verify that my security permissions are working right and that the right data is hidden or shown from my template (which uses the thymeleaf spring security integration). The app itself does work correctly when I run it. I just want to verify it is working with a set of integration tests. You can find

Does the new Spring MVC Test Framework released in Spring 3.2 test the web.xml configuration?

笑着哭i 提交于 2019-11-30 04:00:07
I've read the docs ( http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework ) several times and I can't confirm if the WebApplicationContext context that gets injected when you use the @WebApplicationContext annotation is actually looking at the web.xml. In other words, I want to test my web.xml configuration. The filters and servlet path specifically. But when I configure my test it ignores the web.xml. (e.g. I try a get request on a URL like this /myServletPath/foo and it fails with a 404.) My test: @RunWith