-
@springboottest replaces the old @integrationtest, which includes default configurations such as Auto Configuration, Package Scan, and so on, but without the SpringBootApplication class, Locations = {” CLASspath :/xxx.xml”}) to specify the Context configuration
-
@ SpringBootTest is a large collection, but sometimes don’t want to, all can Overide ways, such as add a @ EnableAutoConfiguration (exclude = {MongoAutoConfiguration. Class}). But if you need to exclude a lot, it will be very troublesome and you don’t want to directly @Springboottest
-
For not adding @springboottest, But need @ ExtendWith (SpringExtension. Class) — — JUnit5, or @ RunWith (SpringJUnit4ClassRunner. Class), junit 4, By default, Spring only reads application-xxx.properties as PropertySource, where XXX = Active Profile Name, and does not read YML. If you want to support reading YML, you need to implement another implementation. Otherwise, at sign Value in code will not inject the corresponding Value correctly
public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory { @Override public PropertySource<? > createPropertySource(String name, EncodedResource resource) throws IOException { if (resource == null){ return super.createPropertySource(name, resource); } return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), null); }}Copy the code
And then you say it through annotations
@PropertySource(value = "classpath:application-test.yml", factory = YamlPropertyLoaderFactory.class)
Copy the code
But personally I find it a bit troublesome
Therefore, I prefer that if @springboottest can be used directly, PropertySource can also be used directly. Yml, and if @springboottest is not used, The test PropertySource can be used directly with. Properties