I want to write an android instrumentation test to persist a RealmObject called 'Dog'. Test works fine if Dog is under /src/main/java folder. But Dog is a class only for testing, so I want to place it under /src/androidTest/java folder
Test should run fine after class is moved under /src/androidTest/java
Started running tests
java.lang.IllegalArgumentException: Dog is not part of the schema for this Realm
My test classes are:
@RunWith(AndroidJUnit4.class)
public class RealmTest {
@Rule
public TestRealmConfigurationFactory configFactory = new TestRealmConfigurationFactory();
Realm realm = null;
@Before
public void setUp() {
RealmConfiguration realmConfig = configFactory.createConfiguration();
realm = Realm.getInstance(realmConfig);
}
@After
public void tearDown() {
realm.close();
}
@Test
public void testPersistEntity() {
realm.beginTransaction();
realm.copyToRealm(new Dog());
realm.commitTransaction();
}
}
public class Dog extends RealmObject{
private String name;
public Dog() {}
}
Realm version(s): 2.2.1
Realm sync feature enabled: no
Android Studio version: 2.1
Which Android version and device: 6.0
In the output from Gradle, do you see a line like Note: Processing class: Dog? Does it help to add @RealmClass to your Dog class?
Do you use apt, or annotationProcessor?
Adding @RealmClass doesn't help, same result. I only see 'Note: Processing class: Dog' when Dog is under /src/main/java folder, if I place this class under /src/androidTest/java this message disappear. I'm not using apt nor annotationProccesor, this is my build.gradle:
````
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
mavenCentral()
}
````
And my top level build.gradle:
````
buildscript {
ext.kotlin_version = '1.0.6'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:2.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'kotlin'
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
````
I can upload the project to a git repo if it helps.
Thanks you very much.
Ah, this is something kapt-related. @zaki50 might know.
Yes, if I remove all kotlin related, test works properly. Is there any workaround for this problem?
We are by no means experts in Kotlin so currently we only have the following advices: https://realm.io/docs/java/latest/#kotlin
We currently don't add dependencies of annotation processor to androidTest when the project is using kapt (this is the case of @vicpinm 's project).
I couldn't recall the reason.
I'll look into it more deeply.
Adding following two lines to your dependencies in the build.gradle solves the problem?
kaptAndroidTest 'io.realm:realm-annotations:2.2.1'
kaptAndroidTest 'io.realm:realm-annotations-processor:2.2.1'
Yes, it's solved!! Thanks you so much!
@vicpinm I've made a PR #4022 to solve this problem.
Once that PR is merged (and you update Realm to new version), current work-around (adding kaptAndroidTest dependencies) might cause an error. Don't forget to remove those lines when you updates Realm.
re-opening this for #4022
Okey, thanks you so much
@zaki50 @kneth
Hello! I'm running into a similar issue detailed below:
I have a kotlin project that uses Realm, and I'm trying to run an espresso test in it.
The issue is, the model objects in the src/main folder are not being processed in the src/androidTest folder when running the espresso test, so I get a
Entity is not part of the schema for this Realm. Error
There is a single test model object in the src/androidTest folder which is being added to the schema, however the rest ones in src/main are not.
Is there a way around this without having to use a realm module? As it's a UI test I want it to mirror the production app as close as possible.
Edit: Seems this is already documented here: https://github.com/realm/realm-java/issues/5586
Most helpful comment
Adding following two lines to your
dependenciesin thebuild.gradlesolves the problem?