vendredi 28 août 2020

Load an external libraries as jar (in resources folder ) when run class.method in a jar dinamically

I have developed a Test console that loads the @Test methods in jars in a specific folder via REFLECTION and runs them as well. Everything works fine but when a Test.jar use an external library internally (in the "resources" folder) it doesn't work, not finding this ext. library.

For example:

I have a TEST jar: SEM1.jar which has inside it in main/reosurces/ extLib1.jar When I run the TEST1 method in the ClassTest.class. I have the following ERROR:

java.lang.NoClassDefFoundError: Lit / xxxx / yy / sdk / mymqtt; (inside extLib1.jar).

The code I use to run the test is the following:

public static void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener) throws ClassNotFoundException, NoSuchMethodException, MalformedURLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Launcher launcher = LauncherFactory.create();
        
        ClassLoader loader = URLClassLoader.newInstance(
                new URL[] { new File(pathJars+"/"+jar).toURI().toURL() },
                ServiceUtil.class.getClassLoader()
        );

        loader.getClass();
        Class cls=loader.loadClass(class_name);
        Constructor constructor = cls.getConstructor();
        constructor.newInstance();

        LauncherDiscoveryRequest request;
        if (test_name.isPresent()) {
            Method m = cls.getMethod(test_name.get());
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectMethod(cls,m))
                    .build();
        }
        else{
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectClass(cls))
                    .build();
        }

        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);
        loader=null;
        System.gc();

    }

Thanks for help me. Regards

Aucun commentaire:

Enregistrer un commentaire