AndroidStudio not automatically importing RoboLectric objects - unit-testing

After setting up gradle properly, when i add for example #RunWith(RobolectricTestRunner::class) at the top of my test class, Andtoid studio does not import the class automatically when I click Alt+Enter.
When I manually type import org.robolectric.RobolectricTestRunner, i see that the class is recognized.
I have already done Invalidate Caches/Restart...
I am using Kotlin and AndroidStudio 4.0 and robolectric:4.3.1.

I had to use
androidTestImplementation 'org.robolectric:robolectric:4.3.1'
instead of
testImplementation 'org.robolectric:robolectric:4.3.1'
i my module build.gradle.

Related

Byte-buddy and cglib not available in Spock test

I started playing around with Spock testing framework and my build.gradle dependencies section looks like this:
dependencies {
testCompile "org.spockframework:spock-core:1.3-groovy-2.5"
}
I have a (useless) Spock test that requires creation of a stub:
def 'A test that will fail'() {
given: ''
def random = Stub(Random)
}
When launched, the test fails with the given error:
CannotCreateMockException: Cannot create mock for class java.util.Random. Mocking of non-interface types requires a code generation library. Please put an up-to-date version of byte-buddy or cglib-nodep on the class path.
This error is mentioned in Spock documentation and it's caused by cglib or byte-buddy not being available at runtime.
Considering that spock-core's pom lists both byte-buddy and cglib as compile dependencies, why are they not retained at runtime? In other words, why do we need to explicitly add the following runtime dependency to our build.gradle file?
testRuntime "net.bytebuddy:byte-buddy:1.9.3"
Because both (byte-buddy and cglib-nodep) are marked as <optional>true</optional>, where from Gradle point of view they are as compileOnly dependencies - see: https://blog.gradle.org/introducing-compile-only-dependencies
Compile-only dependencies are distinctly different than regular
compile dependencies. They are not included on the runtime classpath
and they are non-transitive, meaning they are not included in
dependent projects.
They are optional dependencies, so you need to include them if you want that functionality
https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

How to include a Annotation Processor generated code to test classpath in Java/Kotlin

I have an Android project in process of migration from Java to Kotlin. In this project, I have a pure Kotlin module where I'm implementing a API Client with the following build.gradle:
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}"
implementation "com.squareup.retrofit2:retrofit:${retrofit_version}"
implementation "com.squareup.retrofit2:converter-gson:${retrofit_version}"
implementation "com.google.code.gson:gson:${gson_version}"
implementation "com.squareup.okhttp3:logging-interceptor:${okhttp_version}"
implementation "io.reactivex.rxjava2:rxjava:${rx_java_version}"
implementation "io.reactivex.rxjava2:rxkotlin:${rx_kotlin_version}"
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:${retrofit2_rxjava2_adapter_version}"
compileOnly "javax.annotation:jsr250-api:${jsr250_version}"
implementation "com.google.dagger:dagger:${dagger_version}"
kapt "com.google.dagger:dagger-compiler:${dagger_version}"
testImplementation "junit:junit:${junit_version}"
testImplementation "org.mockito:mockito-core:${mockito_version}"
testImplementation "org.hamcrest:hamcrest-junit:${hamcrest_version}"
testImplementation "com.squareup.okhttp3:mockwebserver:${mockwebserver_version}"
// Dependence injection
kaptTest "com.google.dagger:dagger-compiler:${dagger_version}"
}
There is an Annotation Processor dependency for unit tests:
kaptTest "com.google.dagger:dagger-compiler:${dagger_version}"
I can see the generated sources on build/generated/source/kapt/test directory, but they are not visible to the test sources, that is, is not possible to import the generated DaggerUnitTestComponent to inject dependencies, for exemple. And I'm having trouble to get it done.
This kind of thing I had already done with success on a Android project, with the help of this StackOverflow answer and the following snippet added to build.gradle, but for a pure Kotlin/Java project, it is not applicable.
android.applicationVariants.all {
def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}
Is valid to say that I'm using Android Studio 3.0.1 and Kotlin 1.2.10. The sources of Kotlin library module lies on src/main/java and src/test/java.
You might want to take a look at an example of a Kotlin project that uses dagger: (here)
From what I see, the test sources that use the generated classes should compile just fine during a Gradle build, but the IDE might not pick them up correctly.
Try updating Kotlin to a newer version in the project (1.2.10 should handle this). If that does not help, try using the idea plugin as in the example above, configured for the test generated sources as follows:
apply plugin: 'idea'
idea {
module {
testSourceDirs += file('build/generated/source/kapt/test')
generatedSourceDirs += file('build/generated/source/kapt/test')
}
}

Where is my test helper for ember-power-select?

I am reading the docs for ember-power-select testings here.
On the setup, it says:
import registerPowerSelectHelpers from '../../tests/helpers/ember-power-select';
registerPowerSelectHelpers();
...
I do not see ember-power-select in my tests/helpers/ directory. Am I supposed to install it separately, or was it supposed to come by default?
I installed ember-power-select by running the command suggested: ember install ember-power-select.
How can I use some of the power-select helpers like selectChoose(), selectSearch(), &c. like prescribed on the docs?
If you look ember-cli explanation about addons; there says "test-support/ - merged with the application’s tests/" and if you look at source code of ember-power-select there is a helper directory under test-support directory. This means when you install ember-power-select; this directory behaves like it is merged with your application's tests directory. It does not matter whether you see the tests/helpers/ember-power-select.js under your project. You can access it like this. Let's assume your project is named sample-project; then you can just import relevant function as follows:
import registerPowerSelectHelpers from 'sample-project/tests/helpers/ember-power-select';
from within your acceptance-test and call it before your test begin registerPowerSelectHelpers(); and you are able to use selectChoose(), selectSearch() as you wish.
Similarly you can just import integration test helpers as follows:
import { typeInSearch, clickTrigger } from 'sample-project/tests/helpers/ember-power-select'

Grails: Groovy:unable to resolve class grails.test.mixin.TestFor

I'm a newbie in Grails.
In the unit tests which are generated automatically by Grails, there is import for grails.test.mixin
import grails.test.mixin.TestFor
However, STS complains on Groovy:unable to resolve class grails.test.mixin.TestFor
where is this class defined, and how should I resolve this?
Thanks
As SĂ©rgio Michels suggests, run refresh dependencies on your project. If that doesn't work, run Project -> Clean... on the project.
Also, make sure that you are using a compatible version of grails (ie- version 2.0 or later).

Can I configure Grails to see test classes in my plugin?

I have a large grails project split into several 'in place' plugins to keep things nice and modular. One of my plugins, called 'core services', contains all my domain classes, and also some nice set up work in an abstract test class (adding mock domain instances, etc) that is shared by a number of unit tests.
This is all great for unit tests that also live in the plugin, but I'd like to use that abstract test class to set up mock data in other grails projects' tests that uses that plugin. It doesn't seem that plugins' test classes are included on the classpath when running tests. Is there a way I can tell Grails to include them?:
//this abstract test class is in the core service plugin's test directory. My IDE has no problem with this import
import com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
//this test class is in the project that uses the plugin
class ClaimGiftControllerTests extends AbstractGiftInstanceRelatedUnitTest {
.. my tests
}
And the output (non important parts removed):
| Error Compilation error compiling [unit] tests: startup failed:
...../test/unit/com/myproject/ClaimGiftControllerTests.groovy: 3: unable to resolve class com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
# line 3, column 1.
import com.myproject.coreservices.service.AbstractGiftInstanceRelatedUnitTest
^
You can put your AbstractGiftInstanceRelatedUnitTest into the src/groovy folder of your plugin, instead of the test folder. That way you can include it in the test cases of the plugin and your other projects.