Reputation: 27374
For my Android app I'm writing unit tests that require reading some files. Since those are test-only files I don't want them in my res
folders as I don't want them to end up in my final .apk
file.
I want to do something similar to this question but using the newly added (in Gradle 1.1) unit test support (as opposed to instrumentation test).
My project structure is as follows:
/app
/src
/main
/java/my.module/myClass.java
/res/production_resources_go_here
/test
/java/my.module/myClassTest.java
/resources/testFile.txt
what should my myClassTest
test look like to be able to successfully read the testFile.txt
?
Upvotes: 37
Views: 24049
Reputation: 7581
A better and more general appraoch is the following. It is applicable also in other project types, like Spring. Another benefit is that you don't have to put the file specifically at a place in the package structure. This dependency should not be made (on the package structure). It makes it more readable if you have a good filename (with test folder structure).
this.getClass().getClassLoader().getResourceAsStream(filename);
An example:
private String htmlFromTestResourceFile(String filename) {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
return IOUtils.toString(inputStream, "UTF-8");
} catch( Exception e) {
e.printStackTrace();
return null;
}
}
Upvotes: 2
Reputation: 25050
Followed by @Deepansu's answer, I unified test data for both Test
and AndroidTest
in {project root}/sampledata
directory which is a default location of Android Studio New > Sample Data Directory
command.
1. In your project, right-click and click New > Sample Data Directory
. This will create sampledata
directory in app
, which has same hierarchy as build.gradle
file, src
and build
directories.
2. In build.gradle
, add scripts as below;
android {
sourceSets {
test {
resources.srcDirs += ['sampledata']
}
androidTest {
resources.srcDirs += ['sampledata']
}
}
}
3. Sync
in gradle.
Now, we can put test resource files in one directory and use them in both test environment.
You can read file as below;
// use somewhere at test logic. Note that slash symbol is required (or not).
jsonObject = new JSONObject(readFromFile("/testFile.json"));
// a method to read text file.
public String readFromFile(String filename) throws IOException {
InputStream is = getClass().getResourceAsStream(filename);
StringBuilder stringBuilder = new StringBuilder();
int i;
byte[] b = new byte[4096];
while ((i = is.read(b)) != -1) {
stringBuilder.append(new String(b, 0, i));
}
return stringBuilder.toString();
}
Upvotes: 14
Reputation: 361
Add this to your build.gradle:
android {
sourceSets {
test {
resources.srcDirs += ['src/test/resources']
}
androidTest {
resources.srcDirs += ['src/androidTest/resources']
}
}
}
For resources to be accessible by unit tests, add your files in: src/test/resources
.
And for instrumentation tests, add your files in: src/androidTest/resources
.
Upvotes: 20
Reputation: 11044
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
.
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
Upvotes: 61
Reputation: 53600
I am working a project with an structure similar to what you mentioned.
I put all my server responses in a file under resources
folder like app/src/test/resources/BookingInfo.json
.
All java test files are under app/src/test/java/PACKAGE_NAME
similar to what you said. I have a Fixture
class under a package that contains name of resources:
@SuppressWarnings("nls")
public final class Fixtures
{
public static final String GET_ANNOUNCEMENT = "GetAnnouncement.json";
...
}
And finally a FixtureUtils class that a method of this class is responsible to read resource file and return result.
import java.nio.file.Files;
import java.nio.file.Paths;
public class FixtureUtils
{
public static final AFixture fixtureFromName(final String fixtureName)
{
final String fixtureString = FixtureUtils.stringFromAsset(fixtureName);
if (StringUtils.isEmpty(fixtureString))
{
return null;
}
final Gson gson = new Gson();
final AFixture aFixture = gson.fromJson(fixtureString, AFixture.class);
return aFixture;
}
private static final String stringFromAsset(final String filename)
{
try
{
final URL resourceURL = ClassLoader.getSystemResource(filename);
if (resourceURL == null)
{
return null;
}
final String result = new String(Files.readAllBytes(Paths.get(resourceURL.toURI())),
Charset.forName("UTF-8")); //$NON-NLS-1$
return result;
}
catch (final Exception e)
{
e.printStackTrace();
}
return null;
}
private FixtureUtils()
{
// Ensure Singleton
}
}
And AFixture
class looks like:
public class AFixture
{
public List<JsonElement> validItems;
public List<JsonElement> invalidItems;
public AFixture()
{
super();
}
public List<JsonElement> getInvalidItems()
{
return this.invalidItems;
}
public List<JsonElement> getValidItems()
{
return this.validItems;
}
public void setInvalidItems(final List<JsonElement> invalidItems)
{
this.invalidItems = invalidItems;
}
public void setValidItems(final List<JsonElement> validItems)
{
this.validItems = validItems;
}
}
Note: java.nio.file
has been removed from JDK8 if I'm not mistaken however you have no problem if you are using JDK7. If you are using JDK8 then you just need to change stringFromAsset
method in such a way like FileInputStream (old fashion style) or Scanner.
Upvotes: 1
Reputation: 3350
An example of a correct way to do this would be to put the file in your assets folder. However, the contents of the assets folder will be added to the apk.
InputStream is = resources.getAssets().open("test.txt");
You can cheat this system and traverse to any other file in your project. Be sure to create an assets directory in the location specified in the project's iml file (e.g. src/main/assets).
InputStream is = resources.getAssets().open("../../test/resources/testFile.txt");
An example of a way to get resources would be:
Resources resources = new Activity().getResources();
Upvotes: -3