简体   繁体   中英

How to unit test a library that uses JNI?

Unit testing best practices call for the use of inversion of control to ensure that unit tests run independently of each other even if they depend on singleton instances (each test gets their own "singleton").

What is the recommendation for testing a library that uses JNI to manipulate a native resource that only exists once per Process (eg the terminal)?

I'm not asking about testing the actual JNI code but rather the Java code that depends on the native code as a side-effect.

Its the same as usual.

You create a mock of the library class your code under test (cut) uses and verify the communication between your cut and that mock.

class JniUsageTest{

  @Rule 
  public MockitoRule mockitoRule = MockitoJUnit.rule(); 

  @Mock
  private JniInterfaceClass jni;

  @Test
  public void testJniCommunication_SomethingToWrite_PassedToInterface(){
    // arrange
    doReturn("the Answer from JNI").when(jni).calledByCut(any(Object.class));
    YorClassUnderTest cut = new YorClassUnderTest(jni);

    // act
    String resultContainingJniReturn =  cut.doSomethingExpectedToWtiteToJni();

    // assert
    verify(jni,times(10)).calledByCut(any(Object.class));
    assertThat(resultContainingJniReturn, containsString("the Answer from JNI"));
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM