test involving openmrs.properties file

Hi there,

I am adding a new method to ModuleUtil see

and the PR https://github.com/openmrs/openmrs-core/pull/1836

Im having issues with testing.

My method calls static method

ModuleUtil.matchRequiredVersions(OpenmrsConstants.OPENMRS_VERSION_SHORT, version))

Constant OpenmrsConstants.OPENMRS_VERSION_SHORT is constructed from openmrs.properties file



which contains variables which are replaced by maven on build, but when testing the are just something like “${openmrs.version}”

What I would like is to set the OpenmrsConstants.OPENMRS_VERSION_SHORT to something known, control it during my tests.

My tests are in a BaseContextSensitiveTest class.

Any suggestions ?

Just hack it as below: :smile:

Field versionField = OpenmrsConstants.class.getDeclaredField(“OPENMRS_VERSION_SHORT”);

Field modifiersField = Field.class.getDeclaredField(“modifiers”); modifiersField.setAccessible(true); modifiersField.setInt(versionField, versionField.getModifiers() & ~Modifier.FINAL);

versionField.set(null, “3.0”); //replace 3.0 with any version you want.

1 Like