https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
d

Davide Giuseppe Farella

05/06/2023, 1:24 PM
Hello everybody, not sure if that may be a MockK issue, but let’s start posting here and let’s see 🙂 I have a mapper that maps from
LazyPagingItems
to my model/state (I do that in the presenter, using Molecule, if you’re wondering) In order to test that, I created this convenience mock (since
LPI
has internal constructor)
fun <T : Any> mockLazyPagingItems(isEmpty: Boolean, refresh: LoadState): LazyPagingItems<T> = mockk {
    every { loadState } returns mockk loadState@{
        every { this@loadState.refresh } returns refresh
        every { this@loadState.append } returns LoadState.Loading
        every { this@loadState.prepend } returns LoadState.Loading
    }
    every { itemCount } returns if (isEmpty) 0 else 1
}
Updating to alpha19 I now have this exception (full stacktrace in thread)
java.lang.NullPointerException: Cannot invoke “androidx.compose.runtime.State.getValue()” because “$this$getValue$iv” is null
How would I fix that?
full stack trace
s

shikasd

05/06/2023, 6:41 PM
It seems like LazyPagingItems are reading some state that wasn't initialized because of mockk
d

Davide Giuseppe Farella

05/06/2023, 8:12 PM
I would get a mock exception "no answer found blabla" 🤔 My guess is that that
$iv
is something from the IR, no? 🤔
I didn't have time to dive yet. By the way I think it would be nice having a test artifact that provides a way to create an instance of a
LPI
, but I'm not sure in which scenarios that would be useful besides mine, as it would be impossible to get a
LPI
outside of the UI without Molecule (as
collectAsLazyPagingItems
is the only way)
I dig a bit more, and I assume the problem is that
loadState
is now a delegate. I instantiated a
LPI
using reflection, but I’ve not been able to run my tests correctly, as I’ve not been able to update the snapshot list under test 😞
I think I’ll create a surrogate of
LPI
and create a surrogate mapper in between, so I can test my mapper (
m2
) using the surrogate
LazyPagingItems (m1)-> LazyPagingItemsSurrogate (m2)-> PagingItemsState
i

Ian Lake

05/07/2023, 7:46 PM
I have a mapper that maps from LazyPagingItems to my model/state (I do that in the presenter, using Molecule, if you're wondering)
This is the wrong thing to do in the first place. Map your Flow<PagingData> using the operators provided by Paging at that level
d

Davide Giuseppe Farella

05/07/2023, 7:50 PM
I'm not trying to map the models, I want a declarative state for the UI, which can be: • Empty • Items (also loading or not) • Loading • Error (also loading or not) While LPI provides a set of about 50 combinations or more 😁
i

Ian Lake

05/07/2023, 7:56 PM
I'm a bit confused, since Loading is completely covered by Empty and Items? The number of items and load state are independent properties
Sounds like you just need a Kotlin extension that takes CombinedLoadState and make it into the simplified view of the world you want?
E.g., Loading, Loaded, or Error?
d

Davide Giuseppe Farella

05/07/2023, 7:59 PM
Yes, they're independent, but in my UI logic one depends on the other (eg, when loading I'd show something different regarding whether I have items or not). That was my purpose, but encapsulated in a (generic) mapper class with tests 😊
i

Ian Lake

05/07/2023, 7:59 PM
And I'm still not entirely sure where mocking this comes into the picture? You can create a fake Flow<PagingData> easily enough and just use the actual collectAsLazyPagingItems()?
d

Davide Giuseppe Farella

05/07/2023, 7:59 PM
I did solve with the surrogate tho, a bit verbose, but it does its job
Using the collectAsLazyPagingItems didn't work as expected under JUnit test, at least with my setup
i

Ian Lake

05/07/2023, 8:01 PM
E.g.,
flowOf(PagingData.from(...)).collectAsLazyPagingItems()
d

Davide Giuseppe Farella

05/07/2023, 8:03 PM
The snapshot list was never updated in my case. I also tried using Dispatchers.setMain, but didn't help
i

Ian Lake

05/07/2023, 8:04 PM
Seems like you should have led with "how do I get collectAsLazyPagingItems working in my test" rather than bringing up a problem a few steps down the wrong path - that way we'd avoid that XY Problem: https://en.wikipedia.org/wiki/XY_problem
d

Davide Giuseppe Farella

05/07/2023, 8:05 PM
True 😁 the reason is because the test was working as expected, but the update to alpha19 changed that, so the history conditioned my question
But I was also thinking now that using collectAsLPI would led to another problem: I'd need to test using the Flow coming from the library, which emission is subject to changes: e.g. the first emitted LPI has changed in some way on alpha17
i

Ian Lake

05/07/2023, 8:13 PM
You'll really probably want to wait one more build, since it only then that PagingData.from instantly provide the static state to the first composition if you use it with StateFlow (e.g.
MutableStateFlow(PagingData.from...))
)
Since that'll be the build that this change is released in
d

Davide Giuseppe Farella

05/08/2023, 5:55 AM
Thank you for the info, I'm also super excited about the previews 🥳