0

I have a maven project where I run automated tests only. Under

  src/test/resources 

I have a folder

books

which contains 2 xml files

(src/test/resources/books/1.xml,

src/test/resources/books/2.xml).

During my test I check how many files I have in folder

'books'

int filesNumber = new File(".\\src\\test\\resources\\books").listFiles().length;

Everything works fine localy on my machine when I run my test with maven

"clean test"

The problem starts when I run my tests on TeamCity server because I get a

NullPointer

when it looks for 'books' folder. It just can't find it. Can someone tell me why? Maybe I shouldn't put this folder to the resources directory? If yes then which directory I should use..

Community
  • 1
  • 1
EdXX
  • 872
  • 1
  • 14
  • 32

1 Answers1

1

It is fine to put test resource in src/test/resources, but don't read them as files. Read them as resources, for instance:

// assuming MyTest class in books package
MyTest.class.getResourceAsStream("books1.xml");

Or

MyTest.class.getClassLoader().getResourceAsStream("books/books1.xml");
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • thanks but how I can check hom many files I have? int filesNumber = new File(".\\src\\test\\resources\\books").listFiles().length; – EdXX Sep 01 '17 at 13:24
  • This is tricky. See: https://stackoverflow.com/questions/186496/can-i-list-the-resources-in-a-given-package – lexicore Sep 01 '17 at 13:26
  • You should not need a test to check the number of test resources, your tests should be directly using the 'test resources' to help test your code. – MartinByers Sep 01 '17 at 13:42