How to refer to a project and not jar while running test cases in Maven

35 Views Asked by At

I have a maven Project A which has Project B in its dependencies. Now, the Project A has few test cases which refers to a xml file inside Project B.

When I run this test cases in Project A, through Eclipse, the test case passes. But when I try to run the same test case using Command line, it fails with nullpointer exception.

The Exception comes as JVM is unable to get Java.net.URI.getPath() for the same resource. When running from CMD, the jvm gets the URI of the jar and not the Project and hence it fails to read the content. When running from Eclipse, Eclipse automatically resolves the workspace project from the Project B and get the XML file from its target folder.

I was able to reproduce the same issue in Eclipse by switching off the workspace resolution.

Project B code :

static {
        medRefFileUri = ResourceUtil.getResourceURI(MedRefPDO.class, MEDREF_FILE);
        if (medRefFileUri != null) {
            resourceMgrInstance = new MedRefSourceFileManager();
            regStatus = ResourceMonitor.getInstance().register(resourceMgrInstance, medRefFileUri);
        }
    }


public ResourceManager.RegistrationStatus register(ResourceManager resourceManager, URI uri) {
        if (uri == null) {
            log.error("error registering uri -  null uri");
            return RegistrationStatus.ERROR_NULL_URI;
        }
        String fileName = uri.getPath().replaceAll("%20", " ");
        File configFile = new File(fileName);
    

We are getting null pointer exception on the last second line. Upon debugging, found the URI to be

jar:file:/C:/Users/susengupta/.m2/repository/com/gehcit/cp/ProjectB/1.0/ProjectB-1.0.jar!/com/gehcit/cp/medref/pdo/MedRef.xml

and that is the exact location of the resource file.

As we just want to run Junits, we can easily refer the resource files from Project B's target folder but I am not able to find a way how to configure maven so that it picks up the URI from Project B's target folder without using any IDE. Earlier when we used to run the junits using ANT, we were able to run this test case successfully.

0

There are 0 best solutions below