如何从JUnit内部使用Voldemort服务器?

我正在尝试在Maven项目中使用来自JUnit内部的Voldemort.以下代码编译时,它不会成功执行:

@Before
public void setUp() throws Exception
{
    // Start Voldemort
    VoldemortConfig config = VoldemortConfig.loadFromEnvironmentVariable();
    VoldemortServer server = new VoldemortServer(config);
    server.start();
}

相反,抛出此异常:

java.lang.UnsatisfiedLinkError: voldemort.utils.JNAUtils.mlockall(I)I
    at voldemort.utils.JNAUtils.mlockall(Native Method)
    at voldemort.utils.JNAUtils.tryMlockall(JNAUtils.java:51)
    at voldemort.server.VoldemortServer.startInner(VoldemortServer.java:251)
    at voldemort.server.AbstractService.start(AbstractService.java:62)
    at a.b.c.FunctionalTest.setUp(FunctionalTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
    at $Proxy0.invoke(Unknown Source)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

知道如何解决这个问题吗?

最佳答案 请注意堆栈跟踪中的这一行

at voldemort.utils.JNAUtils.mlockall(Native Method)

这表明无论调用什么代码,都通过JNI使用一些本机库.如果在加载请求加载此类的类时JVM无法加载本机库,则会遇到UnsatisfiedLinkedError.

要确保代码可以加载ddl / so文件,请使用以下java系统属性,

-Djava.library.path=/path/to/directory/containing/native/libraries

或者,您可以将相同的目录添加到Windows上的PATH环境变量或Linux上的LD_LIBRARY_PATH变量.

查看http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html此页面描述了如何将系统属性传递给maven-surefire-plugin,后者负责在Maven中运行测试.

点赞