如何从运行于PowerMockRunner的android单元测试中加载一个json文件?

时间:2022-05-01 00:31:40

I am using PowerMockRunner to run my unit tests. I want to load some canned network response json files from my assets folder.

我正在使用PowerMockRunner运行单元测试。我想从我的assets文件夹中加载一些固定的网络响应json文件。

I am using this method to try to get the file.

我正在使用这个方法来获取文件。

private static File getFileFromPath(Object obj, String fileName) {
        ClassLoader classLoader = obj.getClass().getClassLoader();
        URL resource = classLoader.getResource(fileName);
        return new File(resource.getPath());
    }

I call the method like this from my class which has these annotations at the top.

我从我的类中调用这个方法它的顶部有这些注解。

@RunWith(PowerMockRunner.class)
@PrepareForTest(Network.class)

File file = getFileFromPath(this, "mock_response.json");

However, when I evaluate this expression.

但是,当我计算这个表达式时。

classLoader.getResource(".");

It shows that I am currently in this the directory below while running this test.

它显示在运行这个测试时,我目前在下面的目录中。

/Users/tylerpfaff/Library/Android/sdk/platforms/android-23/data/res/

Seeing as I'm in the platforms resource directory, there is no hope of me successfully loading my resources from my project's resource directory. What do I need to do to access my resource directory of my project?

由于我在platform资源目录中,所以我不可能成功地从项目的资源目录中加载我的资源。我需要做什么来访问我的项目的资源目录?

1 个解决方案

#1


1  

You have two options:

你有两个选择:

  • try to get system classloader via ClassLoader.getSystemClassLoader()
  • 尝试通过classloader . getsystemclassloader()获取系统类加载器
  • Get classloader from the class that has been ignored by PowerMock. By default:
  • 从PowerMock忽略的类中获取classloader。默认情况下:

"org.hamcrest.", "java.", "javax.accessibility.", "sun.", "org.junit.", "org.testng.", "junit.", "org.pitest.", "org.powermock.modules.junit4.common.internal.", "org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate", "org.powermock.core*", "org.jacoco.agent.rt.*"

“org.hamcrest。”“java”。,“javax.accessibility。”“太阳。”,“org.junit。”,“org.testng。”junit。”,“org.pitest。”,“org.powermock.modules.junit4.common.internal。”、“org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate”、“org.powermock。核心*”、“org.jacoco.agent.rt。*”

Or you may use @PowerMockIgnore.

或者可以使用@PowerMockIgnore。

#1


1  

You have two options:

你有两个选择:

  • try to get system classloader via ClassLoader.getSystemClassLoader()
  • 尝试通过classloader . getsystemclassloader()获取系统类加载器
  • Get classloader from the class that has been ignored by PowerMock. By default:
  • 从PowerMock忽略的类中获取classloader。默认情况下:

"org.hamcrest.", "java.", "javax.accessibility.", "sun.", "org.junit.", "org.testng.", "junit.", "org.pitest.", "org.powermock.modules.junit4.common.internal.", "org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate", "org.powermock.core*", "org.jacoco.agent.rt.*"

“org.hamcrest。”“java”。,“javax.accessibility。”“太阳。”,“org.junit。”,“org.testng。”junit。”,“org.pitest。”,“org.powermock.modules.junit4.common.internal。”、“org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate”、“org.powermock。核心*”、“org.jacoco.agent.rt。*”

Or you may use @PowerMockIgnore.

或者可以使用@PowerMockIgnore。