Skip to content

Commit

Permalink
Add TestResourcesRule
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyTangAndroid committed Dec 28, 2020
1 parent 36c24be commit 7d9d01c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
13 changes: 6 additions & 7 deletions tester/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'java-library'
dependencies {
implementation 'commons-io:commons-io:2.6'

testImplementation 'junit:junit:4.12'
testImplementation 'commons-io:commons-io:2.6'
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'org.mockito:mockito-core:2.28.2'
implementation 'junit:junit:4.13.1'
implementation 'commons-io:commons-io:2.6'
testImplementation 'commons-io:commons-io:2.6'
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'com.google.code.gson:gson:2.8.6'
testImplementation 'org.mockito:mockito-core:3.3.3'

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.android.tang.tony.test.util;

import java.io.InputStream;
import org.junit.rules.ExternalResource;

public class TestResourcesRule extends ExternalResource {

String resourceFilePath;

public TestResourcesRule(String resourceFilePath) {
this.resourceFilePath = resourceFilePath;
}

public String content() {
return content("utf-8");
}

public String content(String charSet) {
return TestUtil.content(resourceFilePath, charSet, this);
}

public InputStream inputStream() {
return getClass().getResourceAsStream(resourceFilePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;

public class TestUtilsTest {

@Rule public TestResourcesRule resource = new TestResourcesRule("gson.json");

@Test
public void content() {

String json = TestUtil.content("gson.json", this);
String json = resource.content();
Type type = new TypeToken<Map<String, Integer>>() {}.getType();
Map<String, Integer> t = new Gson().fromJson(json, type);
Truth.assertThat(t).isEqualTo(expected());
Expand Down

0 comments on commit 7d9d01c

Please sign in to comment.