Whenever I try to run unit-tests for classes in my app module that depend on classes from an library module, I get this:
java.lang.NoClassDefFoundError: de/ivu/junittest/DummyData
at de.ivu.junittest.app.DummyModel.<init>(DummyModel.java:16)
at DummyModelTest.testInstantiation(DummyModelTest.java:7)
...
In the above sample, DummyData is part of the lib module, while DummyModel is part of the app module. DummyModel has a member of type DummyData, but instantiating this in the test-class DummyModelTest causes the aforementioned exception at test-time.
The project structure is as follows:
JUnitTestProject
app [module]
src
main
java
de.ivu.junittest.app
DummyModel.java
...
...
test
java
de.ivu.junittest.app
DummyModelTest.java
...
lib [module]
src
main
java
de.ivu.junittest
DummyData.java
...
...
The build.gradle for the app module contains the following:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
sourceSets {
unitTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/res')
}
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project (':lib')
compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.+'
unitTestCompile 'org.robolectric:robolectric:2.+'
unitTestCompile 'com.google.android:android:4.+'
unitTestCompile project (':lib')
unitTestCompile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
instrumentTestCompile 'junit:junit:4.+'
instrumentTestCompile 'org.robolectric:robolectric:2.+'
}
task unitTest(type:Test, dependsOn: assemble) {
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest
And finally the source of the three java-classes, starting with DummyData:
package de.ivu.junittest;
import android.util.Log;
public class DummyData {
private int data;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
The DummyModel class:
package de.ivu.junittest.app;
import android.util.Log;
import de.ivu.junittest.DummyData;
public class DummyModel {
private DummyData data = new DummyData();
public void setData(int data) {
this.data.setData(data);
}
public int getData() {
return this.data.getData();
}
}
And finally, DummyModelTest:
import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.robolectric.RobolectricTestRunner;
import de.ivu.junittest.app.DummyModel;
#RunWith(RobolectricTestRunner.class)
public class DummyModelTest {
#Test
public void testInstantiation() {
DummyModel model = new DummyModel();
model.setData(42);
assertEquals(model.getData(), 42);
}
}
After trying more than a dozen different things, any help is deeply appreciated.
The trick is to add the other modules' classes directories as dependency. So you end up with unitTestCompile files instead of unitTestCompile project:
dependencies {
...
unitTestCompile files("../lib/classes/release")
...
}
Not very beautiful, nor very intuitive, but it works with my current setup (Gradle 1.10, Build Tools 19.0.1, and Android-Gradle-plugin 0.8).
Related
So scoping with Koin DI seem to throw a weird exception when KoinApplication::checkModules() method is called within a unit test. Here is the full code:
import org.koin.core.KoinApplication
import org.koin.core.component.KoinComponent
import org.koin.core.component.KoinScopeComponent
import org.koin.core.component.createScope
import org.koin.core.component.inject
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.core.scope.Scope
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.koin.test.inject
import kotlin.test.BeforeTest
import kotlin.test.Test
class FixScopingTest : KoinTest {
private val component1: Component1 by inject()
private lateinit var koinApp: KoinApplication
#BeforeTest
fun setup() {
koinApp = startKoin {
modules(
module {
single { Component1() }
scope<Component1> {
scoped { Component2() }
}
}
)
// printLogger(Level.DEBUG)
}
}
#Test
fun verifyKoinApp() {
//component1.component2.print()
koinApp.checkModules()
}
}
class Component1 : KoinComponent, KoinScopeComponent {
override val scope: Scope by lazy { createScope(this) }
val component2: Component2 by inject()
}
class Component2 {
fun print() = println("Component2::print()")
}
exception 1:
com.xycompany.xyproj.xypackage.FixScopingTest > verifyKoinApp FAILED
java.lang.IllegalStateException: Missing MockProvider. Please use MockProvider.register() to register a new mock provider
at org.koin.test.mock.MockProvider.getProvider(MockProvider.kt:10)
at org.koin.test.mock.MockProvider.makeMock(MockProvider.kt:23)
at org.koin.test.check.CheckModulesKt.mockSourceValue(CheckModules.kt:102)
at org.koin.test.check.CheckModulesKt.check(CheckModules.kt:95)
at org.koin.test.check.CheckModulesKt.checkAllDefinitions(CheckModules.kt:86)
at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:72)
at org.koin.test.check.CheckModulesKt.checkModules(CheckModules.kt:40)
at org.koin.test.check.CheckModulesKt.checkModules$default(CheckModules.kt:40)
at com.xycompany.xyproj.xypackage.FixScopingTest.verifyKoinApp(FixScopingTest.kt:43)
Second weird issue appears when you uncomment the commented part so we would have usage of scoped components on DEBBUG level logger:
exception 2:
com.xycompany.xyproj.xypackage.FixScopingTest > verifyKoinApp FAILED
java.lang.NoSuchMethodError: 'double kotlin.time.Duration.toDouble-impl(long, java.util.concurrent.TimeUnit)'
at org.koin.core.time.MeasureKt.measureDurationForResult(Measure.kt:41)
at org.koin.core.scope.Scope.get(Scope.kt:189)
at com.xycompany.xyproj.xypackage.FixScopingTest$special$$inlined$inject$default$1.invoke(KoinTest.kt:53)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.xycompany.xyproj.xypackage.FixScopingTest.getComponent1(FixScopingTest.kt:20)
at com.xycompany.xyproj.xypackage.FixScopingTest.verifyKoinApp(FixScopingTest.kt:41)
SETTINGS:
Kotlin Multiplatform Project (test is run in both Andorid and Common packages with the same problem)
VERSIONS:
koin-core: 3.1.3
koin-android: 3.1.3
Looks like you need to add a MockProviderRule when using scoped. It's not required if are not using scopes.
#get:Rule
val mockProvider = MockProviderRule.create { clazz ->
// Mock with your framework here given clazz
// e.g: Mockito.mock(clazz.java)
}
And for it to work you also need to add this dependency to gradle
testImplementation "io.insert-koin:koin-test-junit4:3.1.6"
https://insert-koin.io/docs/reference/koin-test/checkmodules/#allow-mocking-with-a-junit-rule
I need to run my gradle task to test basic functional in the unit test:
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Test;
public class IwillfailyouPluginTest {
#Test
public void applyPlugin() {
final Project project = ProjectBuilder.builder().build();
project.getPlugins().apply(IwillfailyouPlugin.class);
project.task("iwillfailyou").// what method should I run?
}
}
But I can not find the method to run it. Help me, please
My understanding is that ProjectBuilder is more for unit-like tests. So with what you have, you should only be asserting that a task named iwillfailyou exists, is of a certain type, and has the correct configuration.
public class IwillfailyouPluginTest {
#Test
public void applyPlugin() {
final Project project = ProjectBuilder.builder().build();
project.getPlugins().apply(IwillfailyouPlugin.class);
assertTrue(project.getTasks().getNames().contains("iwillfailyou"));
MyCustomTaskType iwillfailyou = project.getTasks().getByName("iwillfailyou");
assertEquals(123, iwillfailyou.getSomeConfig())
}
}
It looks looks you're trying to test the behavior/function of the custom task. For that sort of test, you would use TestKit.
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.io.FileWriter;
import java.nio.file.Files;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class IwillfailyouPluginFunctionalTest {
#Test
void canRunTask() throws IOException {
// Setup the test build
File projectDir = new File("build/functionalTest");
Files.createDirectories(projectDir.toPath());
writeString(new File(projectDir, "settings.gradle"), "");
writeString(new File(projectDir, "build.gradle"), "plugins {" + " id('i.will.fail.you')" + "}");
// Run the build
GradleRunner runner = GradleRunner.create();
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments("iwillfailyou");
runner.withProjectDir(projectDir);
BuildResult result = runner.build();
// Verify the result
Assertions.assertTrue(result.getOutput().contains("someoutput from the iwillfailyou task"));
}
private void writeString(File file, String string) throws IOException {
try (Writer writer = new FileWriter(file)) {
writer.write(string);
}
}
}
I am trying to test a Kotlin class in my common library for my Kotlin Multiplatform project in Android Studio.
I have had to reconfigure the build.gradle file several times and managed to fix most of the unresolved references, but Gradle still can't find the reference for the #Test annotation, while the editor recognizes that it is from the kotlin.test library.
Here is my test class:
import kotlin.test.*
import kotlinx.serialization.json.*
import Recipe
class RecipeTest {
#Test
fun serializeTest() {
val keys = arrayOf("Dessert", "Cookies", "Cute")
val ingredients = arrayOf("12 cups sugar", "2 cups flour", "1 bottle warm love")
val instructions = arrayOf("Sift together in bowl", "Cook however else you see fit!")
val recipe = Recipe(
"Macaroons",
"Morgan",
"Today",
"small cookies",
"1 hour",
keys,
"1 dozen macaroons",
"Dessert",
"French",
false,
ingredients,
instructions,
true
)
val jsonString = JSON.stringify(Recipe.serializer(), recipe)
val obj = JSON.parse(Recipe.serializer(), jsonString)
assertEquals(Recipe.toString(), jsonString)
assertEquals(Recipe.toString(), obj.toString())
}
}
And my module build.gradle file:
plugins {
id("com.android.library")
}
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
android {
compileSdkVersion = 28
buildToolsVersion = '28.0.3'
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
sourceSets {
main {
manifest.srcFile 'src/androidMain/AndroidManifest.xml'
}
}
}
kotlin {
android {
}
iosArm64 {
binaries {
executable()
}
}
iosX64 {
binaries {
executable()
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
implementation kotlin('test')
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
iosMain {
}
}
}
configurations {
compileClasspath
}
When the test in run from command line, the build fails with an exception, saying
Unresolved reference: test
at the line with the #Test annotation.
EDIT: I changed the accepted answer to the one that appears to be most helpful to others, but I'm leaving my old one just in case.
I had a similar issue and found that I needed to explicitly add the platform specific Kotlin test dependencies:
kotlin {
// ...
sourceSets {
commonTest {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-test-annotations-common"
implementation "org.jetbrains.kotlin:kotlin-test-common"
}
}
jvmTest {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-test-junit"
}
}
jsTest {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-test-js"
}
}
}
}
With only the commonTest dependencies I received the "Unresolved reference: test" error. Once I added the jvmTest and jsTest blocks it fixed the error.
As it turns out, I had some conflicting dependencies in my commonTest source set after all. The 'test' dependency was conflicting with the 'test-common', which led to problems that were buried in some build logs. After deleting the extra dependencies, the build succeeded and the test ran. (and passed!)
sourceSets {
...
commonTest {
dependencies {
//only these are needed
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
...
}
I want to make test of some clases in my project.
I'm project is all android, but the new clases are pure Java (I'm trying to make a little sdk for the app)
But I don't know how to configure correctly
Gradle file:
apply plugin: 'android'
android {
compileSdkVersion "Google Inc.:Google APIs:19"
buildToolsVersion "19.0.1"
lintOptions{
checkReleaseBuilds false
}
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 28
versionName "4.0.5"
}
signingConfigs {
debug {
..........
}
release {
..........
}
}
buildTypes {
debug{
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
debuggable true
buildConfigField "boolean", "LOG_ENABLED", "true"
signingConfig signingConfigs.debug
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
debuggable false
buildConfigField "boolean", "LOG_ENABLED", "false"
signingConfig signingConfigs.release
}
}
productFlavors{
develFlavor{
}
testFlavor{
..........
}
trainingFlavor{
..........
}
preFlavor{
..........
}
proFlavor{
.........
}
}
}
if (project.hasProperty('storePassword')) {
android.signingConfigs.release.storePassword = storePassword
}
if (project.hasProperty('keyAlias')) {
android.signingConfigs.release.keyAlias = keyAlias
}
if (project.hasProperty('keyPassword')) {
android.signingConfigs.release.keyPassword = keyPassword
}
repositories {
mavenCentral()
}
sourceSets {
test {
java.srcDir file(''src/main/java/es/tempos/gas/sdk/test'')
}
}
dependencies {
unitTestCompile 'junit:junit:4.11'
compile 'com.google.code.gson:gson:1.7.1'
compile 'com.android.support:appcompat-v7:+'
compile files('libs/libGoogleAnalyticsServices.jar')
compile files('libs/sbc_mapslib.jar')
compile files('libs/t21c2dm-lib-v1.0.jar')
}
I want to put the test cases in the folfer Test>SDK
Estructure of the app:
+SmartPhoneGreatApp
----.idea
----app
-----build
-----libs
-----src
-----develFlavor
-----main
----sdk
-----preFlavor
----- .........
........
-----test
------sdk
and the testing class(for the moment do nothing)
package es.tempos21.gas.sdk.test;
import org.junit.Test;
public class AuthenticateTest {
#Test
public void testAuthenticate() throws Exception {
}
}
And the error I'm getting:
Gradle 'SmartPhoneGreatApp' project refresh failed:
Could not find property 'unitTest' on SourceSet container.
Gradle settings
In the Android plugin, sourceSets are configured differently from how the Java plugin does it, so you should read the docs at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies
For testing you'll want to do something like this:
android {
sourceSets {
androidTest.setRoot('tests')
}
}
A shortcut would be to do this instead:
android.sourceSets.androidTest.setRoot('tests')
Note that you're supplying a new top-level directory for where the tests go (and the Java classes will be in a directory structure underneath that corresponds to their package path); in your example you're trying to point it at a package already inside src/main/java/path/to/package, which isn't going to work.
I need to control the ordering of jars in the testRuntime configuration.
I must make sure that robolectric-x.x.jar comes before android.jar, or else I get the dreaded RuntimeException("Stub!").
How do I do that?
Here is my complete build.gradle for running Robolectric tests against my Android app, which uses RoboGuice:
apply plugin: 'java'
androidJar = new File(System.getenv('ANDROID_HOME'), '/platforms/android-7/android.jar')
configurations { robo }
dependencies {
robo('com.pivotallabs:robolectric:1.0-RC1')
testCompile('org.roboguice:roboguice:1.1.2')
testCompile('junit:junit:4.8.2')
testCompile project (':app')
testCompile files(androidJar)
}
sourceSets.test.compileClasspath = configurations.robo + sourceSets.test.compileClasspath
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath
test {
excludes = ['**/MyRobolectricTestRunner.class']
}
I had to add an exclusion for the test runner, or else Gradle will throw an exception.
MyRobolectricTestRunner.java looks like this:
package com.acme.myapp;
import java.io.File;
import org.junit.runners.model.InitializationError;
import roboguice.application.RoboApplication;
import roboguice.inject.ContextScope;
import com.google.inject.Injector;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
public class MyRobolectricTestRunner extends RobolectricTestRunner {
public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
// Tell Robolectric where to find AndroidManifest.xml and res/
super(testClass, new File("../app"));
}
/**
* Enable injection into tests as well...
*/
#Override
public void prepareTest(Object test) {
RoboApplication myApplication = (RoboApplication) Robolectric.application;
Injector injector = myApplication.getInjector();
ContextScope contextScope = injector.getInstance(ContextScope.class);
contextScope.enter(myApplication);
injector.injectMembers(test);
}
}
And here's a sample test that illustrates injection:
package com.acme.myapp;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import roboguice.inject.InjectResource;
#RunWith(MyRobolectricTestRunner.class)
public class StringFormattingTest {
#InjectResource(R.string.info_pending_amount)
private String pendingAmountPattern;
#Test
public void testFormatInfoPendingAmount() {
String s = String.format(pendingAmountPattern, 20.0d, "EUR");
assertEquals("Only a part of your stake (20,00 EUR) was accepted", s);
}
}
This might work:
configurations { robo }
dependencies {
robo ...
testRuntime ...
}
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath