spring-test

What is the difference between @BeforeClass and Spring @TestExecutionListener beforeTestClass()

我是研究僧i 提交于 2019-11-27 18:07:43
问题 What is the difference between using JUnit @BeforeClass and the Spring @TestExecutionListener beforeTestClass(TestContext testContext) "hook"? If there is a difference, which one to use under which circumstances? Maven Dependencies: spring-core:3.0.6.RELEASE spring-context:3.0.6.RELEASE spring-test:3.0.6.RELEASE spring-data-commons-core:1.2.0.M1 spring-data-mongodb:1.0.0.M4 mongo-java-driver:2.7.3 junit:4.9 cglib:2.2 Using JUnit @BeforeClass annotation: import org.junit.BeforeClass; import

Spring Boot @WebIntegrationTest and TestRestTemplate - Is it possible to rollback test transactions?

北城余情 提交于 2019-11-27 16:44:39
问题 I have a Spring Boot application with Spring Data Rest and I use @WebIntegrationTest along with the TestRestTemplate in my integration tests. The base class for the tests looks something like this: @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles(profiles = "test") @SpringApplicationConfiguration(classes = Application.class) @Transactional @TransactionConfiguration @WebIntegrationTest("server.port: 0") public abstract class IntegrationTest { ... } I was testing the creation of an

@TestPropertySource with dynamic properties

廉价感情. 提交于 2019-11-27 16:30:15
问题 I am using @TestPropertySource to overwrite application.yml properties in my integration test for a spring boot app. @TestPropertySource(properties = { "repository.file.path=src/test/resources/x" }) I was wondering if there was some way to make the property VALUE dynamic. Something like this: @TestPropertySource(properties = { "repository.file.path=PropertyValueProvider.class" }) Your feedback is appreciated. In my case the property value is system specific that should be generated upon the

NoSuchFieldError when trying to run a jUnit test with Spring

痞子三分冷 提交于 2019-11-27 15:04:32
So far I have two tests. One uses only jUnit framework and works fine. The other one uses spring-test library and creates this exception every time I try to run it. Any ideas what may cause the problem? Error java.lang.NoSuchFieldError: NULL at org.junit.runners.ParentRunner.<init>(ParentRunner.java:48) at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:104) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect

How to mock remote REST API in unit test with Spring?

↘锁芯ラ 提交于 2019-11-27 14:33:45
问题 Assume I have made a simple client in my application that uses a remote web service that is exposing a RESTful API at some URI /foo/bar/{baz} . Now I wish to unit test my client that makes calls to this web service. Ideally, in my tests, I’d like to mock the responses I get from the web service, given a specific request like /foo/bar/123 or /foo/bar/42 . My client assumes the API is actually running somewhere, so I need a local "web service" to start running on http://localhost:9090/foo/bar

How to set environment variable or system property in spring tests?

本秂侑毒 提交于 2019-11-27 11:50:36
问题 I'd like to write some tests that check the XML Spring configuration of a deployed WAR. Unfortunately some beans require that some environment variables or system properties are set. How can I set an environment variable before the spring beans are initialized when using the convenient test style with @ContextConfiguration? @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:whereever/context.xml") public class TestWarSpringContext { ... } If I configure the

Overriding beans in Integration tests

笑着哭i 提交于 2019-11-27 10:30:15
问题 For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the RestTemplate as I dont want to connect to external services - I know what responses to expect. I tried providing a different implementation in the integration-test package in the hope that the latter will override the real implementation , but checking the logs it`s the other way around : the real implementation overrides

How to run JUnit SpringJUnit4ClassRunner with Parametrized?

霸气de小男生 提交于 2019-11-27 09:27:58
问题 The following code is invalid due to duplicate @RunWith annotation: @RunWith(SpringJUnit4ClassRunner.class) @RunWith(Parameterized.class) @SpringApplicationConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { } But how can I use these two annotations in conjunction? 回答1: There are at least 2 options to do that: Following http://www.blog.project13.pl/index.php/coding/1077/runwith-junit4-with-both-springjunit4classrunner-and-parameterized/ Your test needs to look

java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

大兔子大兔子 提交于 2019-11-27 07:11:01
I have the following test class: @ActiveProfiles({ "DataTC", "test" }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class }) public class RegularDayToTimeSlotsTest { ... The issue seems to come from the BaseTestConfiguration class: @Configuration @ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class), @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter

How to unit test a Spring MVC controller using @PathVariable?

浪子不回头ぞ 提交于 2019-11-27 05:57:06
I have a simple annotated controller similar to this one: @Controller public class MyController { @RequestMapping("/{id}.html") public String doSomething(@PathVariable String id, Model model) { // do something return "view"; } } and I want to test it with an unit test like this: public class MyControllerTest { @Test public void test() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/test.html"); new AnnotationMethodHandlerAdapter() .handle(request, new MockHttpServletResponse(), new MyController()); // assert something } } The problem is that