-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Improvements to selection of the constructor used in the mock class #115
Comments
Hi @riQQ! Can you submit an example of the code you're having trouble with? I understand that the generated constructor of a Using an |
…uctor is available Fallback to internal constructor if no public constructor is available. Add a test that verifies mocking a class with only an internal constructor works. Part of mockative#115
To verify a function on the class is called the expected number of times. Here's one example from the project I'm working on: class StringMapChangesTest {
@Mock private lateinit var change: StringMapEntryAdd
@Test fun one() {
change = mock(classOf<StringMapEntryAdd>())
every { change.toString() }.returns("x")
every { change.conflictsWith(any()) }.returns(false)
val changes = StringMapChanges(listOf(change))
val someMap = mutableMapOf("a" to "b")
assertEquals("x", changes.toString())
changes.applyTo(someMap)
verifyInvokedExactlyOnce { change.applyTo(someMap) }
changes.hasConflictsTo(someMap)
verifyInvokedExactly(2) { change.conflictsWith(someMap) }
}
fun <R> verifyInvokedExactly(times : Int, block: () -> R): Unit = verify(block).wasInvoked(exactly = times)
fun <R> verifyInvokedExactlyOnce(block: () -> R): Unit = verify(block).wasInvoked(exactly = 1)
} @Serializable
@Mockable
data class StringMapEntryAdd(override val key: String, val value: String) : StringMapEntryChange() {
override fun toString() = "ADD \"$key\"=\"$value\""
override fun conflictsWith(map: Map<String, String>) = map.containsKey(key) && map[key] != value
override fun applyTo(map: MutableMap<String, String>) { map[key] = value }
override fun reversed() = StringMapEntryDelete(key, value)
override fun isValid() = key.length <= 255 && value.length <= 255
} @Serializable
sealed class StringMapEntryChange {
abstract val key: String
abstract override fun toString(): String
abstract override fun equals(other: Any?): Boolean
abstract override fun hashCode(): Int
abstract fun conflictsWith(map: Map<String, String>): Boolean
abstract fun applyTo(map: MutableMap<String, String>)
abstract fun reversed(): StringMapEntryChange
abstract fun isValid(): Boolean
} |
@Nillerr any opinion on the primary constructor topic? |
This works, because the mock is in the same package as the mocked type.
This helps mocking classes with the Serializable attribute, as this creates another constructor that is sometimes chosen by Mockative, which leads to an error.
This is a breaking change, so either needs a new version that allows breaking changes or has to be opt-in via a feature flag in the settings.
I have most of the code ready (minus the feature flag) and am willing to send a PR if this is accepted.
The text was updated successfully, but these errors were encountered: