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?

4 comments
Comment actions Permalink

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.

1
Comment actions Permalink

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));
  }


}

0
Comment actions Permalink

This code is throwing NullPointerException at line PsiFile files = createLightFile 

0
Comment actions Permalink

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

0

Please sign in to leave a comment.