-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test assets not available #2065
Comments
Is |
I have seen this before. Using This works:
|
For what it's worth, you can also put test fixtures in |
@erd how can you share a fixture file, ex: a json file from an api response to mock, between |
I have same behaviour as @nebiros !! Any clue ? Thanks |
@jffiorillo I found this way, to share fixture files from
android {
sourceSets {
String sharedTestJavaDir = 'src/sharedTest/java'
test {
java.srcDirs += [sharedTestJavaDir]
}
androidTest {
java.srcDirs += [sharedTestJavaDir]
resources.srcDirs += ['src/test/resources']
}
}
}
|
Has anyone had any progress with this? |
Thanks @nebiros ! |
EmojiCompat, when used with BundledEmojiCompatConfig, looks for NotoColorEmojiCompat.ttf in the assets directory. If you use gradle As a work-around, I'm trying to explicitly place a copy of NotoColorEmojiCompat.ttf into the assets directory as part of my test build. However, as others have found, files in the UPDATE: When I run The same test, as an androidTest, just works. For now, it looks like EmojiCompat and Robolectric are not easily made to cooperate. (I can't figure out why my normal FontRequestEmojiCompatConfig doesn't work with Robolectric. For some reason, it just never gets out of |
@nebiros I am trying to use your solution. When I run the test in src/androidTest I can see that the resources I'd like to reach were copied to build/intermediates/sourceFolderJavaResources/androidTest/developMock/debug/foo and I'm trying to "find it" using: MyClass.class.getClassLoader().getResource("foo") or getClass().getClassLoader().getResource("foo") [these work from tests under src/test] but the classLoader doesn't find "foo" |
We've revamped resources in Robolectric 4.0 to process the binary resource files + arsc table produced by aapt and the resource handling code is now much closer to that of the framework. This includes changes to parse the merged manifest using the Android platform code. Mind giving this a try with 4.0 + binary resources by configuring your gradle.build as follows:- testImplementation "org.robolectric:robolectric:4.0"
android {
enableUnitTestBinaryResources=true
testOptions {
unitTests {
includeAndroidResources = true
}
}
} We're doing a big bug scrub and trying to be aggressive about it so apologies if this was closed in error, in which case feel free to reopen. If this is still an issue an example project with failing test would be most helpful. |
Ran into this when wanting to test Room db migration from Found this workaround which will copy assets from In the app gradle file: afterEvaluate {
tasks.findByName("packageDebugUnitTestForUnitTest").doFirst {
File testAssetDir = new File("${project.projectDir}/src/test/assets")
File appAssetDir = new File("${project.buildDir}/intermediates/merged_assets/debug/mergeDebugAssets/out")
GFileUtils.copyDirectory(testAssetDir, appAssetDir)
}
} To prevent the assets from going into the apk, they can be excluded by removing the files before tasks.findByName("packageDebug").doFirst {
File dir = new File("${project.buildDir}/intermediates/merged_assets/debug/mergeDebugAssets/out/com.example.app.MyDatabase")
GFileUtils.deleteDirectory(dir)
} This config may be more complex if you run tests with different flavors. Here I just assume running tests with Note, when using the gradle task to copy the assets, I didn't succeed at excluding them from the apk with Note, this is using A less convoluted approach (no gradle task customization) could be:
But then you end up with some files in your main sourceset which are only needed for tests. 🤷♀️ |
Reopening, test assets ought to be merged for |
I've encountered similar issue, when trying to use assets from main source set in tests, while using a flavor that also has it's own assets. This occurs only when target sdk is set below 28. |
@calvarez-ov FYI we are testing Room migrations with Robolectric 4.3 and it seems to be getting the assets as expected. We just had to add: sourceSets {
test.assets.srcDirs += files("$projectDir/schemas".toString())
} in |
@zawadz88 could you share your build.gradle and the migration test class configuration, since i cannot make it run although i have follow your way |
Hi @bopbi, android {
testOptions {
unitTests {
//fixes https://github.com/robolectric/robolectric/issues/3169#issuecomment-312046322
includeAndroidResources = true
}
}
sourceSets {
test.assets.srcDirs += files("$projectDir/schemas".toString())
}
}
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas".toString())
}
} Migration class fragment: @RunWith(MyDbRobolectricRunner::class)
class RoomDatabaseMigrationsTest {
@Rule
@JvmField
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
MyRoomDatabase::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory()
)
@Test
@Throws(IOException::class)
fun `Migrate from 1 to 2 persisting correct data`() {
// given
helper.createDatabase(TEST_DB, 1).apply {
insertData(createData(), this)
close()
}
// when
helper.runMigrationsAndValidate(TEST_DB, 2, true, *MIGRATIONS)
// then
// verify DB content after migration
}
private fun insertData(dbModel: MyDbModel, db: SupportSQLiteDatabase) {
val values = ContentValues()
values.put(ID, dbModel.localId)
// put other values
db.insert(MY_TABLE_NAME, SQLiteDatabase.CONFLICT_REPLACE, values)
}
private fun getMigratedRoomDatabase(): MyRoomDatabase {
val database = Room.databaseBuilder(
ApplicationProvider.getApplicationContext(),
MyRoomDatabase::class.java,
TEST_DB
)
.addMigrations(*MIGRATIONS)
.allowMainThreadQueries()
.build()
helper.closeWhenFinished(database)
return database
}
} |
@zawadz88 doesn't seem to work with the latest AGP - |
Any updates here? As the above comment says I see no effect with |
Hi i am using roboletreci version 3.0 for unit testing my app and have the asssets folder with some files inside under src/test/assets but keep getting fileNotFound.
Here is my test code:
Here is my sourceset and dependencies from my gradle file:
production is a build varient
The text was updated successfully, but these errors were encountered: