Overwrite constants for unit testing


I have a file *.constants.list in my project’s src/main/resources folder in which I declare constants. Is it possible to overwrite some of these constants in unit tests? I tried to overwrite the constants in a file in src/test/resources but it doesn’t work. How are constants files managed and is it possible to overwrite them?



Hi Edward,

Constants are declared in *.constants.list resources, so they are loaded from “left-to-right” as described by the Classpath Load Model.

Are you running tests using Eclipse MicroEJ Application launchers or using MicroEJ Module Manager ?

If you are using MicroEJ Application launchers, the classpath is computed from the configuration of Eclipse Java Projects.

By default, MicroEJ module skeletons do not configure specific output folders, so the content of src/main/java, src/main/resources src/test/java and src/test/resources is copied to the default bin folder. So even if they are declared in two different source folders, your *.constant.list files are copied in the same classpath folder.

There is no guaranteed order the files are loaded within the same classpath entry: it may depend on the host Operating System, the file name or the file modification timestamp for example.

However, it is possible to manually change this default behavior by editing the .classpath file of your project and modify the classpath entries declaration with the following:

		Declare specific output directories for tests, 
		before the main classes and resources 
	<classpathentry kind="src" output="bin-test" path="src/test/java"/>
	<classpathentry kind="src" output="bin-test" path="src/test/resources"/>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="src" path="src/main/resources"/>

You should get the following file content (on the left) compared to the default one (on the right)

Thus, the test classes and resources should be taken into account before the main classes and resources.