spring-test

How to mock Eureka when doing Integration Tests in Spring?

心不动则不痛 提交于 2019-12-09 16:26:36
问题 I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {FrontControllerApplication.class}) @WebAppConfiguration @ComponentScan @IntegrationTest({"server.port:0", "eureka.client.registerWithEureka:false", "eureka.client.fetchRegistry:false"}) @ActiveProfiles("integrationTest") public class MyControllerIT { In the application-integrationTest.properties I have the following

spring boot test unable to inject TestRestTemplate and MockMvc

痴心易碎 提交于 2019-12-09 02:07:43
问题 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

Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

孤人 提交于 2019-12-08 23:44:07
问题 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

spring test not populating database configuration from application.properties

烈酒焚心 提交于 2019-12-08 08:13:01
问题 In my spring boot application, I have the following class that calls a stored procedure public class FmTrfUtil { static int returnVal; public static int insertFmTrfs(List<String> trfs, String source) { EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager(); Session session = em.unwrap( Session.class ); final String[] trfArray = trfs.toArray(new String[trfs.size()]); final String src = source; session.doWork( new Work(){ public void execute(Connection

Test spring rest controller secured with https

痞子三分冷 提交于 2019-12-07 16:43:05
问题 Can anybody provide me with a code sample to write integration test for a controller which is secured with HTTPS?? With HTTP I am able to write, but with HTTPS I am getting certification error. Controller @RestController @RequestMapping("/rest/otm/v1") public class LoginController { @Autowired UserAuthenticationService userAuthService; @ExceptionHandler({ AuthenticationFailedException.class}) @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseBody public void login

Order of bean destruction in Spring context hierarchy

亡梦爱人 提交于 2019-12-07 15:44:40
问题 Is it correct to say that when a Spring context hierarchy is closed, there is no guaranteed order in which the beans will be destroyed? E.g. the beans in the child context will be destroyed before the parent context. From a minimal example the destruction of the contexts seems to be totally uncoordinated between the contexts (oddly enough). Both contexts registers a shutdown hook which later will be executed in different threads. @RunWith(SpringJUnit4ClassRunner.class) @ContextHierarchy({

SpringJUnit4ClassRunner initialize beans for each test?

吃可爱长大的小学妹 提交于 2019-12-07 13:55:01
问题 The following test illustrates that this test bean is initialized twice by Spring. I'm hoping someone can tell me why this is so, since it should only be once. Here's the test: import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.InitializingBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)

spring boot 1.4, spock and application.properties

耗尽温柔 提交于 2019-12-07 06:49:26
问题 I am trying to write some tests for my Spring Boot 1.4.0 with Spock and my application-test-properties file is not being picked up. I have this in my gradle: dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-web') compile 'org.codehaus.groovy:groovy-all:2.4.1' testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org

Spring overriding primary bean with non-primary bean

…衆ロ難τιáo~ 提交于 2019-12-06 17:06:10
问题 I am trying to override a Spring bean during a test declared in a test configuration with the use of @Primary. One declaration is in the src/main/java path, the other, the primary, is in src/test/java path. However, Spring is intentionally replacing the primary bean with the the non-primary bean, the one I don't want to use for the test. If I simply comment out the production (src/main/java) configuration bean, it uses the primary test (src/main/test) bean in the test configuration as desired

Should Mockito be used with MockMvc's webAppContextSetup in Spring 4?

旧巷老猫 提交于 2019-12-06 12:43:39
问题 I'm having difficulties getting Mockito and MockMvc working together when I use the webAppContextSetup together. I'm curious if it's because I'm mixing the two in a way they were never intended. Source: https://github.com/zgardner/spring-boot-intro/blob/master/src/test/java/com/zgardner/springBootIntro/controller/PersonControllerTest.java Here is the test I'm running: package com.zgardner.springBootIntro.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner