====== Setup ====== Add dependency to build.gradle in your app module testImplementation 'org.robolectric:robolectric:4.3' testImplementation 'androidx.test:core:1.0.0' ===== Why robolectric needs to use jdk9 from Q ===== Robolectric 이 AOSP framework build 에 기반해서 만들어지는데, Anroid Q 부터 java build toolchain이 java9로 변경된 관계로 jdk9를 쓸 수 밖에 없다고 함. 관련 링크: https://github.com/robolectric/robolectric/issues/5258 ====== Troubleshooting ====== Android studio 4.0에서 OpenJdk 11 (or 9이상)으로 Robolectric Unit test 실행시 아래의 에러가 날 경우 WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.robolectric.util.ReflectionHelpers$6 (file:/Users/lindol/.gradle/caches/modules-2/files-2.1/org.robolectric/shadowapi/4.3/81dfcf4a45b623b7744e46358d01c7ce054d0fff/shadowapi-4.3.jar) to method java.lang.ClassLoader.getPackage(java.lang.String) WARNING: Please consider reporting this to the maintainers of org.robolectric.util.ReflectionHelpers$6 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [Robolectric] com.example.unittest.MainActivityTest.testSimpleLoad: sdk=28; resources=BINARY Downloading from maven Downloading: org/robolectric/android-all/Q-robolectric-5415296/android-all-Q-robolectric-5415296.pom from repository sonatype at https://oss.sonatype.org/content/groups/public/ Error transferring file: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) [WARNING] Unable to get resource 'org.robolectric:android-all:pom:Q-robolectric-5415296' from repository sonatype (https://oss.sonatype.org/content/groups/public/): Error transferring file: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) 해결 방법: Robolectric 4.4 로 버전 올리기 ([[https://github.com/robolectric/robolectric/issues/5456|4.4 버전에서 수정됨]]) testImplementation 'org.robolectric:robolectric:4.4' ====== References ====== * [[http://robolectric.org|Robolectric]] - official website * [[https://developer.android.com/training/testing/set-up-project|Set up project for AndroidX Test]] * [[https://github.com/robolectric/robolectric/blob/master/robolectric/src/test/java/org/robolectric/shadows/ShadowTelephonyManagerTest.java|ShadowTelephonyManagerTest.java]] * [[http://robolectric.org/extending/|Shadows]] * [[https://meetup.toast.com/posts/187|[Android] Unit Testing Framework - Robolectric]] * [[https://www.codexpedia.com/android/robolectric-unit-test-sample-code-for-android/|Robolectric Unit Test Sample Code for Android]] * [[http://robolectric.org/blog/2019/06/04/paused-looper/|Improving Robolectric's Looper simulation]] * [[https://developer.android.com/reference/androidx/test/core/app/ActivityScenario|ActivityScenario]]