I am able to run each #DataJpaTest separately from eclipse.
My #DataJpaTest is something like below:
#RunWith(SpringRunner.class)
#DataJpaTest
#Import(UserDataOnDemand.class)
#AutoConfigureTestDatabase(replace = Replace.NONE)
public class UserIntegrationTest {
......
}
Is there any way to run all #DataJpaTest classes at once ?
You have two options really.
Include all the tests in the same package and run all the tests in that package.
Include all your #DataJpaTest annotated classes in a test suite class and run this:
#RunWith(Suite.class)
#SuiteClasses({ DataJpaTest1.class, DataJpaTest2.class })
public class MyTestSuite {
}
Related
Maven surefire plugin:
<includes>
<include>**/*Test*.java</include>
Base class:
#autoConfigureMockMvc
#SpringBootTest
#ActiveProfiles("test")
public class ApplicationTests {
}
This class is extended by all test classes, which are usually of following syntex:
#ExtendsWith(SpringExtension.class)
public class ATestClass extends ApplicationTests {
#Test
//test method
}
When I do >mvn clean package
All tests run but for each class the boot application restarts, asif its recreating ApplicationContext for each test (Or something else), so it take an hour for build to complete.
How to run all tests in single go.
I am trying to load application properties in Spring boot project for testing. I am also using #DataJpaTest annotation. Many people suggested to use #TestPropertySource annotation with combination of #datajpaTest but it is not loading properties. It is loading properties if I user #SpringBooTest.
#RunWith(SpringRunner.class)
#DataJpaTest
#TestPropertySource(locations="classpath:application.properties")
public class EntityRepositoryTests {
}
My application properties file is in main/resource/ folder.
It is working if I use #SpringBootTest(properties = { "classpath:application.properties" } but I have
#Autowired
private TestEntityManager entityManager;
which is failing to autoconfigure with SpringBootTest. I have also tried #ContextConfiguration(initializers=ConfigFileApplicationContextInitializer.class) but no luck.
For test you must use
#ActiveProfiles("test")
along with application-test.properties
These are what I just used
#RunWith(SpringRunner.class)
#DataJpaTest(showSql = false)
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
#ActiveProfiles("test")
I try to perform some Cucumber tests of a Spring boot application.
It seems like Spring Boot is not started before tests are running.
What am i missing?
https://bitbucket.org/oakstair/spring-boot-cucumber-example
My Cucumber repo still runs without doing all the above steps:
https://github.com/BarathArivazhagan/Cucumber-spring-integration
Docs : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
To add more:
#SpringBootTest takes care of loading the application context in that case #ContextConfiguration is reductant.
Spring test automatically provides a bean of TestRestTemplate which can be autowired but still it should work with RestTemplate also.
It still runs without RANDOM_PORT but RANDOM port can be also used in conjunction for testing.
Let's say you have a feature file, feature1, and glueCode in org.xyz.feature1
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"pretty"},
features = "src/test/java/resources/feature/feature1",
glue = {"org.xyz.feature1"})
public class CucumberTest {
}
#RunWith(SpringRunner.class)
#SpringBootTest(classes = {Application.class},
webEnvironment = WebEnvironment.RANDOM_PORT)
#ContextConfiguration
#Ignore
#Transactional
public class FeatureTest extends CucumberTest {
#LocalServerPort
int randomServerPort;
#Given("........")
public void test_1 {
}
}
I found the problem and has updated the repo.
I did the following to get it working:
Added RANDOM_PORT to #SpringBootTest
Added #ContextConfiguration
Switched from RestTemplate to TestRestTemplate
I have a spring boot application. In the main class annotated with #SpringBootApplication, I have imported some configurations, using the import annotation.
#SpringBootApplication
#Import({ MyConfiguration.class })
public class MySpringBootApp {
public static void main(String[] args) {
new SpringApplicationBuilder(MySpringBootApp.class).build().run(args);
}
}
Now when I run my junit test class, annotated with "#RunWith(SpringRunner.class)", it loads the application, and the imported configuartion classes in the main class, are also loaded (that is MyConfiguration).
#RunWith(SpringRunner.class)
public class MyTest {
....
}
Is there a way to override the import, so that MyConfiguration is not loaded while running tests.
I understand you need to use a SpringRunner but you want to use a different configuration. In that case you simply annotate your Test class with #ContextConfiguration(classes = SomeConfigurationClass.class)
So it would look like this:
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = SomeConfigurationClass.class)
public class MyTest {
....
}
If you do not want any Spring Container, just remove #RunWith(...)
I have a SpringBoot Application and I a config package with
#Configuration
#EnableJpaAuditing
public class PersistenceConfig {
}
But the PersistenceConfig does not get picked up in a PersonRepositoryTest
#RunWith( SpringRunner.class )
#DataJpaTest
public class PersonRepositoryTest {
// Tests ...
}
However, if I change from #DataJpaTest to #SpringBootTest, PersonRepositoryTest will pick up the config.
My package structure is
- main
- java
- config
PersistenceConfig.java
- domain
Person.java
- persistence
PersonRepository.java
Application.java // #SpringBootApplication
- test
- java
- persistence
PersonRepositoryTest.java
The Testing improvements in Spring Boot 1.4 suggest to test the persistence layer with #DataJpaTest
Observation:
Doing both annotations on the Test class still do not import the config
#SpringBootTest
#DataJpaTest
Question 1:
When testing the Persistence Layer with #DataJpaTest
how do I properly (best practise way in Spring Boot) import the config package into my Tests?
Question 2:
Can it be an acceptable work around using #SpringBootTest? I am aware that #DataJpaTest is also a meta annotation with sensible auto configuration for my database including transaction management. But what If I do not need it?
A solution is to use #Import to import your configuration to the configuration done by #DataJpaTest. This is my understanding of #Import.
#RunWith(SpringRunner.class)
#DataJpaTest
#Import(AuditConfiguration.class)
public class AuditTest {
}
with AuditConfiguration that enables auditing
#Configuration
#EnableJpaAuditing
public class AuditConfiguration {
}
You can try this:
annotate PersistenceConfig with #ComponentScan to enable component scanning in Spring.
#Configuration
#EnableJpaAuditing
#ComponentScan(basePackages = "com.yourbasepackage")
public class PersistenceConfig {
}
With no further configuration, #ComponentScan will default to scanning the same package as the PersistenceConfig class.
And add the #Context-Configuration annotation to tell it to load its configuration from the PersistenceConfig.class.
#RunWith( SpringRunner.class )
#DataJpaTest
#ContextConfiguration(classes=PersistenceConfig.class)
public class PersonRepositoryTest {
// Tests ...
}
After #georges van post I have found out that ALL configuration classes get also picked up by just adding one line in the test:
#RunWith( SpringRunner.class )
#DataJpaTest
#ComponentScan(basePackages = "com.basepackage.config")
public class PersonRepositoryTest {
// Tests ...
}
If someone only wants ONE specific configuration class you can do:
#RunWith( SpringRunner.class )
#DataJpaTest
#ContextConfiguration(classes=MyConfig.class)
public class PersonRepositoryTest {
// Tests ...
}
Or multiple classes with:
#ContextConfiguration(classes={MyConfig1.class, MyConfig2.class})