'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
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
