Reflection helpers
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
TestHelper.kt declares top-level inline functions with reified type parameters, in package
com.irurueta.android.testutils, that call private functions and read or write private properties using Kotlin
reflection (kotlin-reflect). They work in local unit tests (JVM or Robolectric) and in instrumented tests.
Each helper comes in two forms:
-
an extension form called on the object, for example
val value: String? = obj.getPrivateProperty("name"), which uses the static type of the receiver (T::class), and -
an explicit form that takes the class and the instance, for example
getPrivateProperty(MyClass::class, obj, "name").
Calling private functions
| Function | Description |
|---|---|
|
Calls the first function named |
|
Same, cast to |
class Counter {
private var count = 0
private fun increment(by: Int) { count += by }
private fun describe(): String = "count=$count"
}
@Test
fun callsPrivateFunctions() {
val counter = Counter()
counter.callPrivateFunc("increment", 2)
val description: String? = counter.callPrivateFuncWithResult("describe")
assertEquals("count=2", description)
// explicit form
callPrivateFunc(Counter::class, counter, "increment", 3)
}
Functions are looked up by name only: if a class has several overloads with the same name, the first one found is called, so pass arguments that match it.
Reading and writing private properties
| Function | Description |
|---|---|
|
Returns the value of the member property |
|
Sets the property. A |
class Holder {
private var text: String? = "initial"
private val constant: String = "fixed"
}
@Test
fun readsAndWritesPrivateProperties() {
val holder = Holder()
val text: String? = holder.getPrivateProperty("text")
assertEquals("initial", text)
holder.setPrivateProperty("text", "changed")
holder.setPrivateProperty("text", null)
// a private val of an instance is written through its backing field
holder.setPrivateProperty("constant", "changed")
}
Static-like members (objects and companion objects)
The Static variants access members of Kotlin object declarations and companion objects, whose properties are
stored in static JVM fields:
| Function | Description |
|---|---|
|
Calls the first function named |
|
Same, cast to |
|
Reads the JVM field behind the declared property |
|
Writes the JVM field behind the declared property |
private object Settings {
private var mode: String? = "default"
private fun reset(): String { mode = "default"; return "reset" }
}
@Test
fun accessesObjectMembers() {
val mode: String? = Settings.getPrivateStaticProperty("mode")
Settings.setPrivateStaticProperty("mode", "custom")
val result: String? = Settings.callPrivateStaticFuncWithResult("reset")
}
A val declared in an object is backed by a static final field and cannot be changed:
setPrivateStaticProperty throws IllegalAccessException for it. The explicit forms also work on regular class
instances, as the library’s unit tests show (for example getPrivateStaticProperty(TestClass::class, testClass,
"privateProperty")).
Things to know
-
Type arguments:
Rcannot be inferred from the arguments. Declare the variable type (val value: String? = obj.getPrivateProperty("name")) or pass type arguments explicitly. -
The extension forms use the static type of the receiver. If the variable is typed as a supertype, members declared only in the subclass are not found; use the explicit form with the concrete
KClass. -
callPrivateFuncandcallPrivateFuncWithResultlook indeclaredMemberFunctions, so they only find functions declared in that exact class, not inherited ones.
Next: Gesture helpers.