spring-test

@DataJpaTest needing a class outside the test

爱⌒轻易说出口 提交于 2019-12-03 05:49:46
In a SpringBoot application, I would like to do some test about the repository layer. @RunWith(SpringRunner.class) @DataJpaTest public class VisitRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private VisitRepository visitRepository; ... } When I try to run test from VisitRepositoryTest , I get an error about DefaultConfigService Field defaultConfigService in com.norc.Application required a bean of type 'com.norc.service.DefaultConfigService' that could not be found. So this needs to run the Application ? I tried to put a bean of DefaultConfigService in

What are TestExecutionListeners, and what do they do?

不打扰是莪最后的温柔 提交于 2019-12-03 04:45:34
As far as I understand, TestExecutionListeners act like @BeforeClass methods in JUnit. What I don't understand is why I need to use DependencyInjectionTestExecutionListener , TransactionalTestExecutionListener and DirtiesContextTestExecutionListener to use DbUnitTestExecutionListener . Normally without DbUnit, I can create and populate the database. Why suddenly do I need to use these listeners to do some CRUD for my database? TestExecutionListeners provide various types of functionality to tests running in the Spring TestContext Framework. If you are interested in what a particular listener

@RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class)

此生再无相见时 提交于 2019-12-03 01:46:12
I was using @RunWith(MockitoJUnitRunner.class) for my junit test with mockito. But now i am working with spring boot app and trying to use @RunWith(SpringRunner.class) . Does using @RunWith(SpringRunner.class) has any advantages over using @RunWith(MockitoJUnitRunner.class) ? Can i still use feature like @Injectmock , @Mock , @Spy with @RunWith(SpringRunner.class) The SpringRunner provides support for loading a Spring ApplicationContext and having beans @Autowired into your test instance. It actually does a whole lot more than that (covered in the Spring Reference Manual), but that's the basic

How to autowire field in static @BeforeClass?

99封情书 提交于 2019-12-02 20:13:33
@RunWith(SpringJUnit4ClassRunner.class) public void ITest { @Autowired private EntityRepository dao; @BeforeClass public static void init() { dao.save(initialEntity); //not possible as field is not static } } How can I have my service injected already in the static init class? It looks to me that you are trying to populate DB before tests. I would give a try to two options: If you can extract initial scripts to sql file (if that is option for you without using repository bean) you can use this approach and annotate your test with @Sql You can explore DbUnit and here is link to spring dbunit

Spring Boot properties in 'application.yml' not loading from JUnit Test

你说的曾经没有我的故事 提交于 2019-12-02 17:55:34
What am I doing wrong? I'm using this little standalone App which runs and finds my src/main/resources/config/application.yml . The same configuration doesn't work from JUnit, see below: @Configuration @ComponentScan @EnableConfigurationProperties public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class); } } @Component @ConfigurationProperties public class Bean{ ... } The following doesn't work, the same properties in application.yml are not loaded and Bean has only null values: @RunWith(SpringJUnit4ClassRunner.class)

spring Should I use @DirtiesContext on every class

不问归期 提交于 2019-12-02 17:37:58
I have several junit tests, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml", "file:src/main/webapp/WEB-INF/confA.xml", "classpath:/mvc-dispatcher-servlet-test.xml"}) @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class ProductContentControllerTest { ... } Inside a class all tests have to run in the same context (which is the case). But I want all my tests classes to be independent. I was assuming that it was the default behavior, but when I run all the test together, it seems to run too fast. How does it work? Is the

Use @Profile to decide to execute test class

狂风中的少年 提交于 2019-12-02 17:11:10
问题 According to https://stackoverflow.com/a/33042872/4106030 we should not use @Profile to let a spring profile decide whether all tests in a test class shall be executed or ignored. There is stated: @Profile is used to selectively enable a component (e.g., @Service , etc.), @Configuration class, or @Bean method if one of the named bean definition profiles is active in the Spring Environment for the ApplicationContext. This annotation is not directly related to testing: @Profile should not be

How to register a ApplicationEnvironmentPreparedEvent in Spring Test

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 09:51:11
问题 I have a @SpringBootTest and I need to notified via ApplicationEnvironmentPreparedEvent to create a database file if it not exists, because my application database tries to connect to it and it doesn't exists. I was doing this via SpringApplicationBuilder , but in the JUnit I haven't access to this builder. This my current main code: SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(); appBuilder.headless(false); appBuilder.listeners(new ApplicationListener<ApplicationEvent>(

Use @Profile to decide to execute test class

杀马特。学长 韩版系。学妹 提交于 2019-12-02 07:49:12
According to https://stackoverflow.com/a/33042872/4106030 we should not use @Profile to let a spring profile decide whether all tests in a test class shall be executed or ignored. There is stated: @Profile is used to selectively enable a component (e.g., @Service , etc.), @Configuration class, or @Bean method if one of the named bean definition profiles is active in the Spring Environment for the ApplicationContext. This annotation is not directly related to testing: @Profile should not be used on a test class . Is this true? If yes then why? It's true because @Profile effects Spring

How to register a ApplicationEnvironmentPreparedEvent in Spring Test

隐身守侯 提交于 2019-12-02 04:26:10
I have a @SpringBootTest and I need to notified via ApplicationEnvironmentPreparedEvent to create a database file if it not exists, because my application database tries to connect to it and it doesn't exists. I was doing this via SpringApplicationBuilder , but in the JUnit I haven't access to this builder. This my current main code: SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(); appBuilder.headless(false); appBuilder.listeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof