Home » Android » How to use googletest for testing C++ code that calls into java on android?

How to use googletest for testing C++ code that calls into java on android?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on a rather complicated C++ library that I plan to test properly using googletest for Android NDK.

So far I follow the google test example and structure the project like this:

Android.mk:

  LOCAL_PATH := $(call my-dir)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo
  LOCAL_SRC_FILES := foo.cpp
  include $(BUILD_SHARED_LIBRARY)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo_unittest
  LOCAL_SRC_FILES := foo_unittest.cpp
  LOCAL_SHARED_LIBRARIES := foo
  LOCAL_STATIC_LIBRARIES := googletest_main
  include $(BUILD_EXECUTABLE)

  $(call import-module,third_party/googletest)

I build and call the test using a script file:

adb push libs/armeabi/libfoo.so //data/local/tmp/
adb push libs/armeabi/libgnustl_shared.so //data/local/tmp/
adb push libs/armeabi/foo_unittest //data/local/tmp/
adb shell chmod 775 //data/local/tmp/foo_unittest
adb shell "LD_LIBRARY_PATH=//data/local/tmp //data/local/tmp/foo_unittest"

This works fine with any pure C++ code that doesn’t have many references but a lot of my code actually relies on java/jni calls. How can I run googletest with a complete apk file that comes not just with C++ code but also java and resources?

How to&Answers:

Inside your test program, you will have a main() function which looks something like this:

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

This allows you to invoke the test program like any other: just type the name of the executable in a shell.

More info on:

https://github.com/google/googletest