NoClassDefFound error when using jUnit in Eclipse - java

I have created a jUnit test class within Eclipse for Mac OS. I was successfully able to create the class and run the code generated by Eclipse, but when I try to add another class I need into the file I get NoClassDefFound.
It seems to be complaining about XmlPullParser, which is used in one of the methods I added:
#Test(expected= Exception.class) public void testParseNull() throws XmlPullParserException, IOException
{
EntryParser parser = new EntryParser();
parser.parse(null);
}
I saw this post but I don't think it is related.
Here is my build path:

Related

Trying to add a path to classpath through gradle or some other way

I have a gradle/SpringBoot project. I'm trying to run some junit tests that use 3rd party annotations to inject classes. The injected classes are creating a text file (call it 'foo.txt') under $(PROJECT_DIR)/app/build/resources/test. However the same injected class then uses a classLoader.getReource("foo.txt") to try to find the file to try to process it. The classLoader looks like it is searching for foo.text under $(PROJECT_DIR)/build/resources/test and since it can't find it there the 3rd party class throws an exception.
So I somehow either need to make classLoader.getResource() called from the 3rd party class search in $(PROJECT_DIR)/app/build/resources/test for foo.txt or make the 3rd party class create foo.txt in $(PROJECT_DIR)/build/resources/test.
I was thinking the way to resolve this might be to add $(PROJECT_DIR)/app/build/resources/test to the classpath in gradle for the test task. But I don't know how to add a new path to the classpath through gradle. I searched all over google but don't see any examples.
Also, If there is a better solution I'm open to that too, but I have limitations in how I can try to resolve this i.e. assume I can't change the 3rd party app for now, and can't refactor project structure of the project I am creating tests in.
Update:
In case it is helpful here is what the test/s looks like:
#ExtendWith({3rdPartyClassResolver.class})
public class 3rdPartyTest {
//the actual failing test
#Test
#3rdPartyClass(
storageLocation = "https://storage-location.us/",
application = "app1",
version = "latest",
requestor = "app2")
public void testing3rdPartyClass(Map<3rdPartyClass, List<Event>> map) {
System.out.println(); //I don't make it to this point
}
//this is what the 3rd party class is trying to do.
//I'm able to reproduce it using this test. it fails the same way.
#Test
public void writeToFile() throws Exception {
Path pathnio = Paths.get("build/resources/test/foo.txt");
Files.write(pathnio, Collections.singleton("some text"));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("foo.txt");
assertNotNull(resource); //this assert fails
}
}

Eclipse - class file is generated automatically even though java file has compilation error

I have created a simple java file having compilation error(Removed ; in 4th line).
public class Test {
public static void main(String args[])
{
System.out.println("Hi")
}
}
After saving If I see bin folder I can see class file(Test.class) being created.Whereas if we compile the same java code through windows command prompt class file is not created.
Eclipse generated compiled class file (below)
public class Test
{
public static void main(String[] paramArrayOfString)
{
throw new Error("Unresolved compilation problem: \n\tSyntax error,
insert \";\" to complete BlockStatements\n");
}
}
Can you please let know why we see 2 different behavior for the file having compilation error in it.
Eclipse's focus is allowing you to do software development. The behavior you've seen allows you to e.g. start unit tests on parts of the class that doesn't have compile errors to check if existing behavior is still the same at that part of the class while you refactor other parts or add new functionality.

A class from external jar throws exception while loading Resource

Hi I am working on a Maven project having dependency on a external jar which has a class ConfigLoader having following loader() method.
public class ConfigLoader {
public void initialize() {
loader();
}
private static void loader() {
URL configURL = ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
//some other method calls to which configURL is an argument.
}
//other methods of ConfigLoader class
}
and the directory structure is like this -
src
|...main
|.......java
|.......resources
|................dev
|................prod
both dev and prod have a file named runtimeConfiguration.xml
and the code which uses this class is
public class Application {
private Application application;
public static void main(String []args){
application = new Application();
application.invokeConfigLoader();
//additional code
}
private void invokeConfigLoader() {
configLoader.initialize();
}
}
The error I get is
could not find: runtimeConfiguration.xml
and the exception is thrown at the getResource() line in the class from jar.
I have tried adding the dev folder to classpath but still the same error. I want to run this code from linux terminal, and the command I am giving from trunk directory (where all my exernal jars and resource folder sits after maven build) is -
java -cp /resources/dev/*:configuration-loader.jar
I am using intelliJ 2017.2 and also tried to add the resources/dev folder as module dependency, but I keep on getting the same error. The resources folder is added as a library via project structure settings. I tried to search a lot but have not found any question with this issue. Kindly help me out as I am new to this environment based development.
Thanks!
ConfigLoader.getClass().getResource("runtimeConfiguration.xml"); will try to get runtimeConfiguration.xml from the same package a the ConfigLoader is defined and not from the root of classpath. Try appending / to runtimeConfiguration.xml.
This should work ConfigLoader.getClass().getResource("/runtimeConfiguration.xml"); or ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml"); depending how you are adding resources to your classpath.
See javadoc for more.

Getting resource files when executing custom maven plugin

I have my custom maven plugin, which has to run tests programmatically on a test phase for example. So I have something like that
#Mojo(name = "aggregate", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class AcceptanceTestMojo extends AbstractMojo {
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
TestExecutor testExecutor = new TestExecutor();
testExecutor.setTestClasses(new Class[]{TestClass.class});
testExecutor.run();
}
}
So the problem comes because the TestClass.class is from another maven module and actually the resources which I want to get are loaded in that module classpath. In that TestClass I have the following method:
public Object[][] retrieveFile() throws IOException {
String[] issuesKeys = IOUtils.toString(
Thread.currentThread().getContextClassLoader().getResourceAsStream("fileName"))
.split("\\n");
....
....
}
If I build the module where the TestClass belongs to everything is working fine, because Thread.currentThread().getContextClassLoader() is loading the correct ClassLoader, but if run it with my plugin as I run the test programatically Thread.currentThread().getContextClassLoader() is loading the classpath of my plugin, so the file I want to retrive is not there and a RuntimeException is thrown.
So my question is how to get the correct ClassLoader so that to be able to get the file or is there a way to load files in classpath manually with java?
I found one solution- I modified the currentThread ClassLoader- I got all required classes from the classpath of the maven project I am executing the plugin on. This is possible with injecting the MavenProject bean:
#Component
private MavenProject project;

ClassNotFoundException when loading a class at runtime

Using the Bukkit-API I'm currently trying to create a plugin which can compile java code of a given pastebin link at runtime and execute that code. Everything is working so far except for one thing: I'm getting a ClassNotFoundException when I'm trying to access one of the already loaded plugins. (I'm not getting an exception when I'm using Bukkit-API methods!)
All plugin jars have been added to the classpath; it currently looks like this:
/home/cubepanel/test-network/jars/craftcubbit.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeHub.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeCore.jar:
The classes I tried to load dynamically:
ClassNotFoundException for MutliCube
import be.multicu.core.MultiCube;
public class Test{
public void debug() {
System.out.println(MultiCube.getInstance());
}
}
Working
import org.bukkit.Bukkit;
public class Test{
public void debug() {
System.out.println(Bukkit.getClass().getName());
}
}
Sourcecode of RuntimeCompiler.java: http://paste.multicu.be/axigofekip.avrasm (It's a bit long, thats why I used a pastebin link)
I also noticed that I'm getting an compilation error when I removed the classpath of MultiCube.jar which means that the classpath must be correct since the sourcecode can be compiled.
EDIT: I was able to fix it by adding MultiCube.class.getClassLoader() as an argument in the constructor of my URLClassLoader

Categories

Resources