spring-mvc-test

Error message = Required MultipartFile parameter 'file' is not present

被刻印的时光 ゝ 提交于 2019-12-23 15:13:25
问题 Testing a spring file upload form, the controlelr signature looks like this @RequestMapping(value = "upload", method = RequestMethod.POST) @ResponseBody public void upload(@RequestParam("file") MultipartFile multipartFile) {} and the test this final MockMultipartFile file = new MockMultipartFile("content", "myFile.txt", "text/plain", "hello".getBytes()); MockHttpServletRequestBuilder mockHttpServletRequestBuilder = .fileUpload("/upload/") .file(file) .accept(MediaType.APPLICATION_JSON); but I

Failed to load ApplicationContext for JUnit test of Spring controller

99封情书 提交于 2019-12-21 03:08:07
问题 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

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required in spring mock mvc test for spring boot with mybatis

六眼飞鱼酱① 提交于 2019-12-14 00:35:44
问题 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/ mybatis-spring-boot-starter1.3.2/mybatis-spring-boot-starter-test and other basic components problems: 1、when i'm use spring mock mvc to testing the controller of spring boot, it appear exception like this:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 2、now i couldn't

stubbing two methods in a mock is throwing Exception using Mockito and Spring MockMVC

允我心安 提交于 2019-12-12 19:24:00
问题 I am trying to set up some simple MVC Unit Testing. What I am trying to accomplish is this: Use Spring MockMVC to test HTTP status and HTTP data returned from my controllers. My controllers have an Autowired reference to a facade which performs the actual logic. So, I create one Test class to test one controller. I want to test two methods of that controller, so I created two methods in my Test class. I've mocked my facade and inject it in my controller. So, I want to configure this mock with

How can I use a Spring HttpRequest in my controller?

大憨熊 提交于 2019-12-12 18:22:11
问题 I've set up my test this way @SpringBootTest @AutoConfigureMockMvc @RunWith( SpringRunner.class ) public class PublicControllerTest { @Autowired private MockMvc mvc; this is my controller signature @GetMapping( produces = MediaTypes.HAL_JSON_VALUE ) ResponseEntity<ResourceSupport> index( final HttpRequest request ) { now it seems to be injecting a proxy value, but if you call request.getURI() for example, it seems to be null. I'm trying to do this so I can pass request to UriComponentsBuilder

Unit testing spring mvc controller with mockito

半腔热情 提交于 2019-12-12 18:15:31
问题 i'm using mockito and junit and run unit test against spring mvc my flow of code is: Service Layer -> Model Layer -> Controller Layer i was successfully testing the Controller against the model layer with the code: @RunWith(MockitoJUnitRunner.class) public class HashLinkerControllerTest { private static final Logger LOGGER = Logger .getLogger(HashLinkerControllerTest.class); @Mock private HashLinkerModel modelMock; @InjectMocks private HashLinkerController controller; private MockMvc mockMvc;

@MockMvc does not work with validation annotated with @Valid

雨燕双飞 提交于 2019-12-12 16:33:06
问题 None of these solutions helped here: Spring mockMvc doesn't consider validation in my test. Added all of these dependencies, nothing helps. I use Spring Boot 2.1.2, pom.xml: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web<

MockMvc: forwardedUrl is null

爱⌒轻易说出口 提交于 2019-12-11 01:10:01
问题 I am having problems to make this test work. Assertion always fails because it can't verify forwardedUrl which is always null. If I remove this assumption, it can't verify return model. I suppose it is due to missing forwarded Url. My test class looks like this: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:test-application-config.xml"}) @WebAppConfiguration public class UserControllerTest { private MockMvc mockMvc; @Mock private UserService

Spring boot test: Test passes, but should not (false positive)

若如初见. 提交于 2019-12-10 19:22:04
问题 I have Spring Boot project with test that does not fail (but should). Am I doing something wrong or it is a issue with Spring? For an small example I have created a small project with 2 entities (User and Category) and one Controller class that has DELETE method (https://github.com/sk8ter/demo). Category entity has an id of User entity without cascade option, so it should fail while deleting a User that has category: @Entity @Table(name = "user") public class User { @Id @GeneratedValue

Testing Spring MVC Router with MockMVC

扶醉桌前 提交于 2019-12-10 02:43:49
问题 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