spring-test

Is it OK to use SpringRunner in unit tests?

梦想与她 提交于 2019-12-06 12:10:26
问题 We are arguing about this approach with my colleagues. They say to use SpringRunner only on integration or functional levels. The question is what pros and cons of using it in level below? For example I have simple bean: public class RewardDurationCalculator { private Clock clock; public OptionalLong calculate(DurationType durationType, List<Pass> passes) { long now = Instant.now(clock).getEpochSecond(); switch (durationType) { case FULL_PASS: return getCurrentPassDuration(passes, now); case

Spring tests are closing embedded database multiple times

与世无争的帅哥 提交于 2019-12-06 11:52:17
问题 I am using h2 embedded database which is defined like that: <jdbc:embedded-database id="embeddedDatasource" type="h2"/> And I have two tests: @RunWith(SpringJunit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("classpath:h2-context.xml") class Test1 {...} @RunWith(SpringJunit4ClassRunner.class) @ContextConfiguration("classpath:h2-context.xml") class Test2 {...} After all tests execution I do see in the log: * Closing org.springframework.context.support.GenericApplicationContext

Spring-test and ServletContextListener in web.xml

感情迁移 提交于 2019-12-06 05:11:55
问题 i try to use spring-test(3.2.10) and integration tests with TestNG by this link. I created RootTest.java @WebAppConfiguration @ContextConfiguration("file:src/test/resources/root-context2.xml") public class ReferenceServiceTest extends AbstractTestNGSpringContextTests { ... spring context loaded success. But my global variables not instantiated because web.xml ignored. In web.xml i have my own "listener-class"(implementation of ServletContextListener) and "context-param". How i can load web

SpringJUnit4ClassRunner initialize beans for each test?

扶醉桌前 提交于 2019-12-05 22:17:09
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) @ContextConfiguration(locations = {} ) public class TestAfterPropsSet implements InitializingBean {

Spring test injection not working when using TestExecutionListener

一曲冷凌霜 提交于 2019-12-05 12:46:51
问题 I want to use a custom TestExecutionListener in combination with SpringJUnit4ClassRunner to run a Liquibase schema setup on my test database. My TestExecutionListener works fine but when I use the annotation on my class the injection of the DAO under test no longer works, at least the instance is null. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext-test.xml" }) @TestExecutionListeners({

How can I prevent my Spring Boot Batch application from running when executing test?

ⅰ亾dé卋堺 提交于 2019-12-05 12:45:42
I have a Spring Boot Batch application that I'm writing integration tests against. When I execute a test, the entire batch application runs. How can I execute just the application code under test? Here's my test code. When it executes, the entire batch job step runs (reader, processor, and writer). Then, the test runs. @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = BatchApplication.class)) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class }) public class

spring boot 1.4, spock and application.properties

℡╲_俬逩灬. 提交于 2019-12-05 10:35:35
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.spockframework:spock-spring:1.0-groovy-2.4') { } Then I have this in /src/test/groovy/resources: # JWT Key

How to drop in-memory h2db between Spring Integration tests?

北城以北 提交于 2019-12-05 10:24:16
I am using Liquibase in my Spring web application. I have a bunch of entities with hundreds of tests for REST APIs in the integration tests for each entity like User, Account, Invoice, License etc. All of my integration tests pass when run by class but a lot of them fail when ran together using gradle test . It is very likely there is data collision between the tests and I am not interested in spending time to fix clean up data as of now. I prefer dropping the DB and context after every class. I figured I could use @DirtiesContext at class and so I annotated by test with it. @RunWith

Error testing spring boot 1.4 application

安稳与你 提交于 2019-12-05 09:19:30
I have very simple spring boot 1.4 application with one rest controller. When i tried to test the context loading of this i am getting error like below :- 13:29:03.623 [main] DEBUG o.s.t.c.j.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.byteobject.cloudsanthe.frontendservice.FrontendServiceApplicationTests] 13:29:03.628 [main] DEBUG o.s.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 13:29:03.632 [main] DEBUG o.s.test.context

org.hibernate.service.UnknownServiceException: Unknown service requested

≡放荡痞女 提交于 2019-12-05 07:42:57
I am writing a unit test to for my AbstractHibernateRepository save method. I'm using spring test runner but I get the following exception when it runs: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201) My Test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("file:src/main/webapp/WEB-INF/spring/spring-hibernate.xml") public class AbstractHibernateRepoTest extends AbstractHibernateRepo