Junit test run in Eclipse but fails with `gradle test` - java

I'm using Spring Boot 2 and I'm trying to do an integration test.
I configured a custom application.properties this way:
#TestPropertySource(
locations = "classpath:###/$$$/application-integrationtest.properties"
)
#ComponentScan(basePackages = { "###.$$$" })
File is under src/test/java/###/$$$/application-integrationtest.properties
Running Junit under Eclipse works fine, but if I try gradle test, I get:
java.io.FileNotFoundException: class path resource
[###/$$$/application-integrationtest.properties] cannot be opened
because it does not exist
What's the deal?

Ok, I put my property file under src/test/resources and added the folder to the build path in Eclipse.
PS: my previous answer was wrong. If I name the file application.properties, the default one has the precedence and it's loaded instead of the test one.

Related

Jenkins Gradle test fails on Commons Configuration

I have a project that uses Apache Commons Configuration. The project is built using gradle. I have some unit test cases written on this project and gradle test works fine when run locally.
However when the unit tests are run in Jenkins it fails.
Please see the screenshot of the error. The error seems to be to do something with Commons Configuration that I am using. Please help.
org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
org.apache.commons.configuration.ConfigurationException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: No ConfigurationProvider registered for tag disabledAdministrativeMonitors
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:752) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.initCombinedConfiguration(DefaultConfigurationBuilder.java:628) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:560) ~[commons-configuration-1.6.jar:1.6]
The ConfigurationProvider try to load a configuration xml file which is default the config.xml.
In my case the project is build using Jenkins. Jenkins provides a config.xml in Jenkins home dir. This is loaded first instead of my desired one. Maybe that applies for you too?
Example Jenkins config.xml
<?xml version=’1.1' encoding=’UTF-8'?>
<hudson>
<disabledAdministrativeMonitors>
...
</disabledAdministrativeMonitors>
...

Spring integration test module running app from separate module without properties for DB config

Given a spring boot gradle module named "md-core" with a Spring application runner and a PostgresDbConfig to connect it to the DB.
The app runs perfectly fine and resolves the properties from the "application.yml" file in the #Config.
#Value("${db.default.pool.size}")
Integer maxPoolSize;
Now, a separate module called "integrationtests" tries to launch the Spring Boot Runner in the "md-core" module.
#ContextConfiguration(classes = {MdCore.class})
#RunWith(SpringJUnit4ClassRunner.class)
public class GivenTest
The test launches the "md-core" Spring runner, but when trying to resolve the properties in the #Config, it does not find any properties.
I've tried directly resolving the "application.yml" in the test. It does not send the properties over to the "md-core" module. Any attempt of adding the "application.yml" properties in the test resolves them to the test file, but does not send them over when the "md-core" module is accessed.
Are there any definitions which should be given in the test via annotations or in the gradle build files?
The classpath when launching the test does not contain the "md-core" resources location, only the classes.
Could this be a reason? If yes, how can the resources be referenced in the classpath of the gradle build files?
I apologize for any mistakes or incomplete information, this post is being written at the end of a work day, hoping there will be answers by morning.
Given this situation, the solution to use the application.properties file for the integration test is the simple addition of initializers = ConfigFileApplicationContextInitializer.class in the #ContextConfiguration annotation of the Test class:
#ContextConfiguration(
initializers = ConfigFileApplicationContextInitializer.class,
classes = {SomeApp.class})
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
public class SomeIntegrationTest {
#Test
public void testTheIntegration() {
// some integration tests here
}
}
Answer also documented in a post on my blog and initially found less detailed on another stackoverflow question.

Spring boot Configuration FileNotFoundException when extending configuration from another spring boot app

I have 2 spring boot apps, and I want one of them to extend the configuration properties of the other. Everything works fine in Eclipse when I run the project as a maven spring boot app, however, when I run the jar or mvn spring-boot: run, I'm getting:
org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class [com.xyz.integration.app.my.Application]; nested exception is java.io.FileNotFoundException:
class path resource [com/xyz/integration/app/pdfthing/ConfigurationManager.class] cannot be opened because it does not exist
I've searched all over and tried many variations, including:
#ComponentScan (using both com.xyz.. and classpath)
#Import
Class-Path in Manifest to the external jar file (In Linux, how to execute Java jar file with external jar files?)
Here's part of the configuration class:
#Component
#ComponentScan("classpath:com/xyz/integration/app/pdfthing")
public class MyConfigurationManager extends ConfigurationManager{
I'm guessing running as a jar doesn't load the external jar in the configuration. Any thoughts/hints? Thanks!

How to test not finding files on classpath? java (spring #Configuration)

I would like to test that a spring
#Configuration class
can handle missing files on the classpath. E.g. when using PropertySourcesPlaceholderConfigurer. But this is just a specific example, the question is really about how to test classes that interact with the classpath (e.g. read a file located in src/main/resources in a maven project).
So in essence I would like to create a spring context where I control the classpath in the test set up code.
The test needs to be a JUnit test.
Hope below may help you
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})
public class YourTestClass{
you have to create a spring context for your test and you can include the production one into it. you can replace classpath*: with a absolute location.
Regards, Rajib.
This work if it's a maven project:
move the classpath file that you want to test the absence from to a separate pom jar module, and include it wherever needed.
move the classpath test to a separate pom jar module named missing-classpath-file-test, but don't include the module with the file that you want to simulate as missing. I will be missing from the classpath only for that test.
When running missing-classpath-file-test, the file will not be on the classpath, and the error you need to reproduce is achieved.
Concerning the question on the comment bellow, with the class loaders that come with application servers and the one used on a junit test it's not possible to programmatically change the classpath.

Junit class - Failed to load ApplicationContext

I am working on the Spring Framework. and made one junit class
but i am not able to properly load the xml files needed to run the #Test method in junit class. In my case
xml file are placed under folder WEB-INF
the junit test class is under test/<package_name>
Please suggest me right way to declare the xml files in
#ContextConfiguration
#ContextConfiguration( locations={ "classpath:/applicationContext.xml",
"classpath:/applicationDatabaseContext.xml" })
Error :
Caught exception while allowing TestExecutionListener
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener#48fa48fa]
to prepare test instance [] java.lang.IllegalStateException: Failed to
load ApplicationContext
If you are using Maven (recommended) then placing your Spring configuration files in the standard location src/main/resources (and src/test/resources for any test-specific configuration), then during the build these files will be copied to the target/classes directory.
You can reference these in your #ContextConfiguration with simply:
#ContextConfiguration(locations = { "/applicationContext.xml",
"/applicationContext-test.xml"})
If you're not using Maven, I'd still recommend using the Standard Directory Layout for source and artifacts, and making your (presumably Ant-based) build process work in a similar manner.

Categories

Resources