单元测试加载本机库的Java类 [英] Unit test Java class that loads native library
问题描述
我在Android Studio中运行单元测试。我有一个Java类,使用以下代码加载本机库
I'm running unit tests in Android Studio. I have a Java class that loads a native library with the following code
static
{
System.loadLibrary("mylibrary");
}
但是当我在 src /中测试这个类时test
我得到的目录
But when I test this class inside my src/test
directory I get
java.lang.UnsatisfiedLinkError: no mylibrary in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
如何让它找到原生.so库的路径位于 src / main / libs
以便进行单元测试而没有错误?
How can I make it find the path of native .so libraries which is located at src/main/libs
in order to unit test without errors?
注意:在<$ c $里面c> src / main / libs 目录我还有3个子目录: armeabi
, mips
和 x86
。其中每一个都包含正确的.so文件。我正在使用非实验版来构建NDK库。
Note: inside src/main/libs
directory I have 3 more subdirectories: armeabi
, mips
and x86
. Each one of those contains the proper .so file. I'm using the Non experimental version for building NDK libs.
我不想使用其他第三方测试库作为我的其他所有纯java类可以进行单元测试。但是,如果那是不可能的话,我会接受其他选择。
I don't wanna use other 3rd party testing libraries as all my other "pure" java classes can be unit tested fine. But if that's not possible then I'm open to alternatives.
这是我的测试代码,它会抛出错误
Here is my test code which throws the error
@Test
public void testNativeClass() throws Exception
{
MyNativeJavaClass test = new MyNativeJavaClass("lalalal")
List<String> results = test.getResultsFromNativeMethodAndPutThemInArrayList();
assertEquals("There should be only three result", 3, results.size());
}
推荐答案
我发现的唯一解决方案没有黑客的工作是通过仪器测试(androidTest目录)使用JUnit。
我的类现在可以在Android设备或模拟器的帮助下正常测试。
The only solution I found that works without hacks is to use JUnit through instrumentation testing (androidTest directory). My class can now be tested fine but with help of the android device or emulator.
这篇关于单元测试加载本机库的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!