spring-test

spring boot test unable to inject TestRestTemplate and MockMvc

回眸只為那壹抹淺笑 提交于 2019-12-01 02:39:36
I am using spring boot 1.4.0.RELEASE . I am writing tests for my controller class. I get the following exception. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.concur.cognos.authentication.service.ServiceControllerITTest': Unsatisfied dependency expressed through field 'restTemplate': No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Maven: NoClassDefFoundError: org.springframework.test.context.junit4.SpringJUnit4ClassRunner

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:33:30
I have a classpath issue. Background: Building and running a Spring WebApp. Originally it was one big project including dao/service/controller/webapp. I have just broken my project into a maven module project essentially splitting the webapp from the dao and service layers. Now my webapp junit tests do not run. The junit code has not changed and I don't think the dependencies have changed (albeit shifted around). Spring-test is in my local repository. Spring-test is in my unit test runtime classpath (used mvn debug logging to check) I have no other classpath issues. All dao module tests run

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

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 use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner?

微笑、不失礼 提交于 2019-11-30 17:29:34
I am testing a Spring Boot application. I have several test classes, each of which needs a different set of mocked or otherwise customized beans. Here is a sketch of the setup: src/main/java: package com.example.myapp; @SpringBootApplication @ComponentScan( basePackageClasses = { MyApplication.class, ImportantConfigurationFromSomeLibrary.class, ImportantConfigurationFromAnotherLibrary.class}) @EnableFeignClients @EnableHystrix public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } package com.example.myapp.feature1;

spring-data-jpa bean validation in junit tests

烈酒焚心 提交于 2019-11-30 15:06:00
问题 In my recent work I've used spring-data-jpa to take advantage of provided repositories. When it came to integration tests I am unable to configure (I assume) spring context for testing and as a outcome bean validation doesn't work in my tests. I am aware that I can inject validator, and unit test my annotations but it's not the case. I am writing integration tests and would like to test repositories with DB backed. I have prepared simple project to show all necessary project files. When I run

spring-data-jpa bean validation in junit tests

谁说我不能喝 提交于 2019-11-30 13:27:52
In my recent work I've used spring-data-jpa to take advantage of provided repositories. When it came to integration tests I am unable to configure (I assume) spring context for testing and as a outcome bean validation doesn't work in my tests. I am aware that I can inject validator, and unit test my annotations but it's not the case. I am writing integration tests and would like to test repositories with DB backed. I have prepared simple project to show all necessary project files. When I run tests, 2 are failing and I have no idea why, hibernate validator is present on class path. Failed

How to clean up mocks in spring tests when using Mockito

↘锁芯ラ 提交于 2019-11-30 13:00:08
问题 I'm pretty new to Mockito and have some trouble with clean up. I used to use JMock2 for unit tests. As far as I know, JMock2 preserves the expectations and other mock information in a context which will be rebuilt for every test method. Therefore every test method is not interfered by the others. I adopted the same strategy for spring tests when using JMock2, I found a potential problem with the strategies I used in my post: The application context is rebuilt for every test method and

Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

一个人想着一个人 提交于 2019-11-30 11:08:04
I have a Bean defined in a class decorated with @Configuration: @Configuration public class MyBeanConfig { @Bean public String configPath() { return "../production/environment/path"; } } I have a class decorated with @TestConfiguration that should override this Bean : @TestConfiguration public class MyTestConfiguration { @Bean @Primary public String configPath() { return "/test/environment/path"; } } The configPath bean is used to set the path to an external file containing a registration code that must be read during startup. It is used in an @Component class: @Component public class