How to get project side files in tests
Answered
I am writing unit tests for my plugin, in which i am getting pom.xml file of user's project in psi file and later casting it to xml file. How can i get this file in tests?
Please sign in to leave a comment.
I'm sorry I don't understand what the problem is. Please share your code or more details about your current test setup and what does not work exactly.
class POMXMLTest extends BasePlatformTestCase {
@Override
protected String getTestDataPath() {
return "src/test/java/demo/testdata";
}
@Test
void getfileTest() throws IOException {
File file = new File("src/test/java/demo/testdata/correctpom.xml");
FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName("pom.xml");
String content = Files.asCharSource(file, Charsets.UTF_8).read();
System.out.println(content);
PsiFile files =createLightFile(fileType, content);
System.out.println(files);
POMXML pom = new POMXML();
// DependencyDetails dependencyDetails = pom.getfile(fi);
// List<String> artifactIdListTest =
// Arrays.asList("asm-util", "sisu-inject-bean", "parboiled-java", "xercesImpl");
// List<String> groupIdListTest =
// Arrays.asList("org.ow2.asm", "org.sonatype.sisu", "org.parboiled", "xerces");
// List<String> versionListTest = Arrays.asList("4.1", "1.4.2", "1.1.4", "2.9.1");
Assert.assertEquals(1, 1);
// Assertions.assertTrue(
// Iterables.elementsEqual(dependencyDetails.artifactIdList, artifactIdListTest));
// Assertions.assertTrue(Iterables.elementsEqual(dependencyDetails.groupIdList,
// groupIdListTest));
// Assertions.assertTrue(Iterables.elementsEqual(dependencyDetails.versionList,
// versionListTest));
}
}
This code is throwing NullPointerException at line PsiFile files = createLightFile
Please use provided methods from test fixture to create and add files to the test environment. There's usually no reason to manually use anything like Java's File API at all. See https://plugins.jetbrains.com/docs/intellij/test-project-and-testdata-directories.html#testdata-files