question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InstrumentalTests: Unresolved reference: create

See original GitHub issue

Version

ext {
    screenshotTestVersion = '0.6.0'
    kotlinVersion = '1.2.41'
    litho = '0.15.0'
}

Issues and Steps to Reproduce

I create test according to https://github.com/facebook/screenshot-tests-for-android/blob/master/sample/src/androidTest/java/com/facebook/testing/screenshot/sample/ExampleScreenshotTest.java

e: ../app/src/androidTest/java/kt/test/instrumental/screenshots/specs/TestLandingSpec.kt: (26, 33): Unresolved reference: create
:kt-app:compileDevStagingAndroidTestKotlin FAILED

Link to Code

Spec

@LayoutSpec
object LandingSpec {

    @OnCreateLayout
    fun onCreateLayout(context: ComponentContext) : Component {
        return Row
                .create(context)
                .backgroundRes(R.color.color_white)
                .widthPercent(100f)
                .heightPercent(100f)
                .child(
                        Image.create(context)
                                .drawableRes(R.drawable.ic_splash_screen)
                )
                .justifyContent(YogaJustify.CENTER)
                .alignItems(YogaAlign.CENTER)
                .build()
    }
}

Test

class TestLandingSpec {

    @Test
    fun testLanding() {
        val context = InstrumentationRegistry.getInstrumentation().targetContext

        val li = LayoutInflater.from(context)

        val lithoView = li.inflate(R.layout.test_litho_view, null, false) as LithoView

        val component = Landing.create(lithoView.componentContext).build()

        lithoView.setComponent(component)

        ViewHelpers.setupView(lithoView).setExactWidthDp(360).setExactHeightDp(640).layout()
        Screenshot.snap(lithoView).record()
    }
}

But it works when:

  • i create component without generated code val component = LandingSpec.onCreateLayout(lithoView.componentContext)
  • i create screenshot of whole activity with this component

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pavlosptcommented, May 22, 2018

Okay I managed to find a solution for your case! There seems to be a bug somewhere (not sure where though).

Solution is consisted of two parts:

  1. You need to add kaptAndroidTest 'com.facebook.litho:litho-processor:0.15.0' so that it can invoke the kapt AP for androidTest related tasks as well!

  2. Regarding the issue I mentioned above, it seems that generated files are not properly copied over to kapt generated sources folder.

Add the following on your app’s module build.gradle:

task copyTestClasses(type: Copy) {
  from "build/generated/source/kapt/debug"
  into "build/generated/source/kapt/debugAndroidTest"
}

afterEvaluate {
  compileDebugAndroidTestKotlin.dependsOn copyTestClasses
}

What this does, is before running compileDebugAndroidTestKotlin task it copies the generated .java files over to the androidTest folder, in order for the test to be able to find those classes on runtime.

Edit 1: Seems that this won’t work properly when combined with clean, so we modify the copyTestClasses task in order to force connectedDebugAndroidTestKotlin to wait for the classes to be copied, before executing.

task copyTestClasses(type: Copy) {
    from "build/generated/source/kapt/debug"
    into "build/generated/source/kapt/debugAndroidTest"
    mustRunAfter "kaptDebugAndroidTestKotlin"
}
1reaction
pavlosptcommented, May 22, 2018

Hey @just-kip seems that indeed ./gradlew clean connectedDebugAndroidTest does not work properly. I have modified the copyTestClasses task in order to make it run always after kaptDebugAndroidTestKotlin and since compileDebugAndroidTestKotlin depends on it, it waits until it copies those classes over, before running.

Here is the modified task:

task copyTestClasses(type: Copy) {
    from "build/generated/source/kapt/debug"
    into "build/generated/source/kapt/debugAndroidTest"
    mustRunAfter "kaptDebugAndroidTestKotlin"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unresolved reference: testing - android - Stack Overflow
InstrumentationRegistry within the test directory for a JUnit test. The error is 'Unresolved reference: test'. Here is the solution to my issue ...
Read more >
Android Studio is reporting Unresolved reference, in wrong ...
Android Studio is reporting Unresolved reference, in wrong test scope. It seems, that Android Studio is mixing InstrumentationTest and UnitTest dependencies ...
Read more >
MPP: Unresolved reference during ... - YouTrack
To create problem, perform these steps: In IDEA, use new project wizard to create multiplatform mobile application for android and ios. Any name,...
Read more >
Learning unit testing on Android: Unresolved reference - Reddit
Hi, I'm learning unit testing on Android. I picked this tutorial as starting point. Now I have this unit test: import org.junit.Assert.
Read more >
Getting "unresolved reference" error when the reference can ...
I am developing and Android app using Android Studio. While trying to run some JUnit tests for my database, I am getting multiple ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found