Hey everyone! I am looking to decouple a dependenc...
# getting-started
p
Hey everyone! I am looking to decouple a dependency from a Kotlin JVM project. The change should make
kotlin-reflect
a
compileOnly
dependency instead of
api
, so the published artifact no longer propagates it automatically. Downstream consumers who want data class marshall/unmarshall must add
kotlin-reflect
explicitly; those who don't are unaffected. The first call to a data class operation should throw a custom exception with a clear message if
kotlin-reflect
is absent. So far, so good. Here comes the tricky part: the testing situation. Normal
test
tasks run with
kotlin-reflect
present (all tests pass). However, as it is now, I cannot have tests that would catch the exception when this dependency is missing. I have three scenarios: (a) tests that need
kotlin-reflect
present in the classpath to succeed, (b) tests that need
kotlin-reflect
absent from the classpath to succeed, and (c) tests that have nothing to do with data classes, so they work regardless. Does anyone have suggestions on how I could tackle this? Thanks! kodee happy
y
You can use
jvm-test-suite
. Simply create a new test suite,
associateWith(sourceSets.test.get())
on its kotlin source set, and add
kotlin-reflect
to it. Alternatively, you can do some runtime magic to make a class loader that throws
ClassNotFoundException
for classes in
kotlin.reflect.full
.
gratitude thank you 1
e
classloaders are designed to be parent-first so you can't make a child classloader that changes or removes classes in the parent (without hacks)
gratitude thank you 1
as long as it's not a KMP project, the jvm test suite is definitely easier
gratitude thank you 1
y
For KMP,
testRuns
would work well enough btw I meant making a custom classloader that overrides
loadClass
and throws. It's kind of a hack, but it's also just the API surface that's available lol
p
Thanks, friends! I will check the JVM test suite. 😉