mockmvc

Rest Assured + Mock MVC @ControllerAdvice

主宰稳场 提交于 2019-12-03 20:51:25
In my project I am using Rest Assured MockMVC with the following dependency: <dependency> <groupId>com.jayway.restassured</groupId> <artifactId>spring-mock-mvc</artifactId> <version>2.9.0</version> </dependency> And my test class looks like: TestController testController = new TestController(); @Before public void configureRestAssuredForController() { RestAssuredMockMvc.standaloneSetup(testController); } I have a couple of ExceptionHandlers defined in the controller class. In my JUnit tests I could verify the request paths and the handlers when defined in the controller class. However - When I

Hamcrest with MockMvc: check that key exists but value may be null

我是研究僧i 提交于 2019-12-03 13:46:37
I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null. { "keyToNull": null, # This may be null, or a String "keyToString": "some value" } The following works for me, but I'm wondering if there's a way to combine each group of two expectations into a single line, as I have a lot of attributes to check: import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.hamcrest.Matchers.*; .andExpect

Spring Security, JUnit: @WithUserDetails for user created in @Before

谁说胖子不能爱 提交于 2019-12-03 10:33:55
In JUnit tests with Spring MockMVC, there are two methods for authenticating as a Spring Security user: @WithMockUser creates a dummy user with the provided credentials, @WithUserDetails takes a user's name and resolves it to the correct custom UserDetails implementation with a custom UserDetailsService (the UserDetailsServiceImpl ). In my case, the UserDetailsService loads an user from the database. The user I want to use was inserted in the @Before method of the test suite. However, my UserDetailsServiceImpl does not find the user. In my @Before , I insert the user like this: User u = new

How to test if JSON path does not include a specific element, or if the element is present it is null?

六月ゝ 毕业季﹏ 提交于 2019-12-03 04:09:11
I have been writing some simple unit testing routines for a simple spring web application. When I add @JsonIgnore annotation on a getter method of a resource, the resulting json object does not include the corresponding json element. So when my unit test routine tries to test if this is null (which is the expected behavior for my case, I don't want the password to be available in json object), test routine runs into an exception: java.lang.AssertionError: No value for JSON path: $.password, exception: No results for path: $['password'] This is the unit test method I wrote, testing the

SpringMVC 测试框架 MockMvc 天坑一枚

穿精又带淫゛_ 提交于 2019-12-01 15:31:16
最近使用SpringMvc写一个URL比较复杂的RequestMapping,最后使用MockMvc测试下URL。 出现的问题是根本不存在的URI,居然返回200。使用以下代码测试: @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration({ "classpath:spring/spring-web.xml" }) public class SearchControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setUp() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void testURLMapping() throws Exception { String[] testCase= { "/asdf" // 根本不存在 }; for (String url : testCase) { this.mockMvc.perform

What is the difference between MockMvc and WebTestClient?

心不动则不痛 提交于 2019-12-01 04:09:13
When I tried to test at Spring 4.x, I used MockMvc web client, but I am reading and trying new features of Spring 5.x. I think, WebTestClient and MockMvc are same or very similar. What is the difference between MockMvc and WebTestClient ? I am waiting for your answer. Thank you nobar Similarities Both provide a fluent -style syntax for testing web services. Both can or do operate in a simulated environment that bypasses the use of HTTP. Major Differences WebTestClient can also be used to test real web services using HTTP. Specify @SpringBootTest instead of @WebFluxText . WebTestClient only

What is the difference between MockMvc and WebTestClient?

♀尐吖头ヾ 提交于 2019-12-01 01:12:19
问题 When I tried to test at Spring 4.x, I used MockMvc web client, but I am reading and trying new features of Spring 5.x. I think, WebTestClient and MockMvc are same or very similar. What is the difference between MockMvc and WebTestClient ? I am waiting for your answer. Thank you 回答1: Similarities Both provide a fluent-style syntax for testing web services. Both can or do operate in a simulated environment that bypasses the use of HTTP. Major Differences WebTestClient can also be used to test

Unit Testing /login in Spring MVC using MockMvc

☆樱花仙子☆ 提交于 2019-11-30 23:41:29
问题 I have a very simple REST application created using Spring MVC. (Code is available at GitHub.) It has a simple WebSecurityConfigurer as follows: @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .csrf().disable() .exceptionHandling() .authenticationEntryPoint(authenticationEntryPoint) .and() .authorizeRequests() .antMatchers("/user/new").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login").permitAll() .successHandler

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

RestFuse vs Rest Assured vs MockMVC Rest Service Unit Test Framework

ⅰ亾dé卋堺 提交于 2019-11-30 18:45:11
I've been trying to find a simple all purpose unit test framework for Spring MVC based Rest Services I've written. I've been searching online and narrowed it down to: RestFuse ( http://developer.eclipsesource.com/restfuse/ ) Rest Assured ( https://github.com/jayway/rest-assured ) MockMVC ( http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/ ) I like RestFuse because it's mostly annotation based, but rest assured seems to have a easier way of passing parameters and checking responses. And finally being a Spring MVC Rest Service project,