How to read a test-only file in Android unit test

At the time when this question was asked this simply wasn’t working. Fortunately this has been fixed since.

You have to put your text file under the app/src/test/resources folder as the OP was trying to do. Additionally it has to be in the same package as your test class. So if you have ReadFileTest.java in package com.example.test in app/src/test/java folder, then your test file should be in app/src/test/resources/com/example/test.

test folder structure

Then you can get to your text file like this:

getClass().getResourceAsStream("testFile.txt")

This opens an InputStream for the text file. If you’re not sure what do with it, here are some of the many ways you could use it: Read/convert an InputStream to a String

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)