Explicit Java tests set the Manifest property

Is there a way during testing to introduce a property into the Java manifest (or enter the entire manifest)?

We read the value from the manifest (version number), which allows null during testing.

So far, we have tried to put the hard MANIFEST.MF file in our test root, but it did not work.

This is the code we use to read the manifest:

private Attributes getManifest() { URLClassLoader cl = (URLClassLoader) getClass().getClassLoader(); Manifest manifest; try { URL url = cl.findResource("META-INF/MANIFEST.MF"); manifest = new Manifest(url.openStream()); } catch (IOException e) { throw Throwables.propagate(e); } return manifest.getMainAttributes(); } 

As a last resort, we will consider the functionality that reads the manifest and scoffs at it, but these are integration tests, and they should be a black box (i.e. we avoid ridicule).

Additional Information: Java 7, running Junit tests in IntelliJ or from Gradle.

+7
java junit
source share
1 answer

You might want to try the jcabi-manifestests library: http://manifests.jcabi.com/ . This is an abstraction of the Java manifest facility and allows you to add new data or even combine multiple manifests at run time.

A typical use would be to access the Manifests.DEFAULT singleton, which contains your MANIFEST.MF applications at run time. You can add to this object:

 Manifests.DEFAULT.put("Test-Property", "Hello"); 

Manifests Javadoc: http://manifests.jcabi.com/apidocs-1.1/com/jcabi/manifests/Manifests.html

Now that you have access to Manifests.DEFAULT again, it will have a "Test-Property" entry. Note that Manifest.DEFAULT implements the Map interface:

 System.out.println(Manifests.DEFAULT.get("Test-Property")) // Prints "Hello" 
+2
source share

All Articles