christophsturm
06/30/2021, 9:44 AMMattia Tommasone
07/01/2021, 8:17 AMVivek Modi
07/05/2021, 1:51 PMviewModel.onDownloadErrorLiveData.observe(this, { consumable ->
some code
})
Vivek Modi
07/12/2021, 4:06 PMinternal fun graphView(viewGroup: ViewGroup, orientation: graphOrientaion = GraphOrientaion.HORIZONTAL): View {
return when (orientation) {
GraphOrientaion.HORIZONTAL -> {
LayoutInflater.from(viewGroup.context).inflate(R.layout.horizaontal_layout, viewGroup, false)
}
GraphOrientaion.VERTICAL -> {
LayoutInflater.from(viewGroup.context).inflate(R.layout.vertical_layout, viewGroup, false)
}
}
}
i added this test cases
@Test
fun `graphView - HORIZONTAL view`() {
val mockOrientation = GraphOrientaion.HORIZONTAL
every {
LayoutInflater.from(mockContext).inflate(
R.layout.horizaontal_layout,
mockViewGroup,
false
)
} returns mockView
// EXECUTION
val view = graphView(mockViewGroup, mockOrientation)
// VERIFICATION
assertEquals(mockView, view)
}
what i am doing wrong java.lang.NullPointerException
at com.vivek$graphView - HORIZONTAL view$1.invoke(TestClass.kt:306)
at com.vivek$graphView - HORIZONTAL view$1.invoke(TestClass.kt:31)
Jilles Soeters
07/28/2021, 5:00 PMmock.fn()
to return 3thana
08/13/2021, 3:55 PMimport io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import javax.persistence.Entity
import javax.persistence.Id
@Entity
data class FooEntity(@Id val id: Long)
@Repository
interface FooRepository : JpaRepository<FooEntity, Long>
class Footest {
private val fooRepository = mockk<FooRepository>()
@BeforeEach
fun setUp() {
every { fooRepository.save(any()) } throws RuntimeException()
}
@Test
fun test() {
val fooEntity = FooEntity(4321)
print(fooEntity.id)
}
}
christophsturm
08/17/2021, 3:52 PMIppei Mukaida
08/20/2021, 10:50 AMTimeZone.currentSystemDefault()
at a below code, but error occurs, java.lang.NoClassDefFoundError: kotlinx/serialization/KSerializer
When mockkObject(TimeZone)
was called, the error happened.
Why happens error?
Enviroments
• kotlinx.datetime 0.2.1
• kotlin-stdlib-jdk7 1.5.21
• mockk 1.12.0
class MyTest {
@Before
fun setup() {
mockkObject(TimeZone)
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun test() {
every {
TimeZone.currentSystemDefault()
} returns TimeZone.of("Asia/Tokyo")
assertEquals(TimeZone.currentSystemDefault(), TimeZone.of("Asia/Tokyo"))
}
}
Aug 20, 2021 7:43:20 PM io.mockk.impl.log.JULLogger warn
WARNING: Failed to transform class kotlinx/datetime/TimeZone$Companion
java.lang.NoClassDefFoundError: kotlinx/serialization/KSerializer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
...
Patrick Ramsey
08/25/2021, 11:56 PMDavide Giuseppe Farella
08/27/2021, 1:39 PMIntent
here, without creating a new mockk()
?
mockkConstructor(Intent::class) {
every {
anyConstructed<Intent>().setDataAndType(any(), any())
} answers {
TODO("return same Intent")
}
}
tim
09/01/2021, 12:58 PMobject Singleton {
val client = SomeClient()
fun foo() {
// calls to me are mocked in my unit test
client.bar()
}
}
When I mock calls to foo
with mockkObject
using every
the SomeClient() constructor is still called. So do I also need to mock SomeClient, or is there a way I can mockkAll of Singleton or perhaps just intercept calls to client?Oleksandr Bashkatov
09/02/2021, 7:59 AMmockkObject(S3)
every { uploadObject(any(), any(), any()) } returns Unit
If I call S3.uploadObject()
from the test function it's mocked like a charm. But given integration tests this object method is called by other services and behavior is not stubbed.
Original object is
object S3 {
// some content
fun uploadObject(stream: InputStream, key: String, metadata: ObjectMetadata): Unit {}
}
Eric Ampire [MOD]
09/08/2021, 7:17 PMKlitos Kyriacou
09/12/2021, 9:36 PMevery { mockObj.methodXYZ() } returns "..."
IntelliJ IDEA occasionally puts red lines underneath every
and on hovering it says "Cannot access class 'io.mockk.MockKStubScope'. Check your module classpath for missing or conflicting dependencies". And it shows "unresolved reference: returns" obviously because that's a method of MockKStubScope which it isn't accessing.
This is only a problem in the editor. If I do a "Build" or "Rebuild", it builds successfully (but the red line under every
still remains). Sometimes, when I invalidate caches and restart, the problem goes away, but not always.
My dependency is:
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>1.12.0</version>
<scope>test</scope>
</dependency>
The class MockKStubScope is defined in mockk-dsl-jvm-1.12.0.jar, which is a transitive dependency. Is this a known issue, or am I using mockk incorrectly?Daniel Branco
09/16/2021, 2:01 PMJustin Tullgren
09/17/2021, 5:26 PMdiego-gomez-olvera
09/22/2021, 12:39 PM@file:JvmName
and refer to it with mockkStatic("<jvm-name>")
, which is not type safe: In case of refactoring (e.g. rename or package change), the tests might break inadvertently. Is there a more type safe manner to mock extension functions?Recca Chao
09/29/2021, 2:16 PMchi
10/11/2021, 4:46 PMimplementation("io.mockk:mockk-common:1.2.0")
in my common test dependencies> Could not resolve all files for configuration ':shared:debugUnitTestRuntimeClasspath'.
> Could not find io.mockk:mockk-common:1.2.0.
Ravin Jain
10/14/2021, 7:00 AMany<File>().outputStream()
I am trying this
val mockOs = mockk<FileOutputStream>()
mockkStatic("kotlin.io.FilesKt__FileReadWriteKt")
mockkStatic(Files::class)
val resultFile = mockk<File>()
every { resultFile.path } answers {"somepath"}
every { any<File>().outputStream() } returns mockOs
But always gets this error
(No such file or directory)
java.io.FileNotFoundException: (No such file or directory)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
Any idea ?Vivek Modi
10/21/2021, 2:54 PMval time = item.time?.substring(0, 10)
Matteo Mirk
10/23/2021, 1:58 PMGiorgio Vespucci
10/27/2021, 10:15 AMMockK
and Jacoco
in a Maven
project?
I have a couple of unit #kotest in which I inject some @Mockk
dependencies.
Until the Jacoco Maven Plugin 0.8.3
all runs smoothly, but when I upgrade it from the 0.8.4
to the latest 0.8.7
then one dependency is not injected by the
MockKAnnotations.init(this)
and some NPE is raised in the tests.
Thankschristophsturm
10/27/2021, 11:43 AMnilTheDev
10/29/2021, 12:10 PMpackage mock
import io.mockk.mockk
internal class DummyDatabaseTest {
val mockConnection: DummyJdbiConnection = mockk()
every { mockConnection.status() } returns "This is JDBI conncetion"
}
Here is my build.gradle
file,
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.31'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
testImplementation 'io.kotest:kotest-assertions-core:4.6.3'
testImplementation 'io.mockk:mockk:1.12.0'
}
Please see the attached screenshot to find out about the error I'm getting.
Can anyone point out where is it going wrong? Do I have to do some manual imports?Slackbot
11/02/2021, 9:12 AMMykola Gurov
11/14/2021, 2:27 PM@Primary
bean - a convenient technique to spy on Beans and inject "strange" behaviors.
All works good till we hit Spring AOP-proxied beans, e.g @Cacheable
in my example. Some funky interactions are happening there and we end up seeing this exception 🧵:Mattia Tommasone
11/16/2021, 9:24 AM@MockK
and using MockkAnnotations.init(this, relaxed = true)
Mendess
11/26/2021, 3:34 PMGil Goldzweig
12/02/2021, 12:02 AMmockkStatic
to mock AppCompatDelegate
and it works well.
I use the same function to test all 3 themes
private fun testTheme(themeMode: Int, radioButtonId: Int) {
System.out.println("Testing theme: $themeMode")
selectedTheme = slot()
every {
setDefaultNightMode(capture(selectedTheme))
} answers {
System.out.println("Setting theme: ${selectedTheme.captured}")
}
onView(withId(radioButtonId)).perform(ViewActions.click())
assertEquals(themeMode, selectedTheme.captured, "Selected theme")
assertEquals(themeMode, selectedMode, "SharedPreferences")
}
public static final int MODE_NIGHT_FOLLOW_SYSTEM = -1;
public static final int MODE_NIGHT_NO = 1;
public static final int MODE_NIGHT_YES = 2;
The RadioButton
is just to select the right theme and call the check.
These are the test cases
@Test
fun `test clicking on light radio button applies light theme`() {
initialize {
testTheme(MODE_NIGHT_NO, R.id.fragment_theme_selection_radio_button_light)
}
}
@Test
fun `test clicking on dark radio button applies dark theme`() {
initialize {
testTheme(MODE_NIGHT_YES, R.id.fragment_theme_selection_radio_button_dark)
}
}
@Test
fun `test clicking on follow system radio button applies system theme`() {
initialize {
testTheme(MODE_NIGHT_FOLLOW_SYSTEM, R.id.fragment_theme_selection_radio_button_system)
}
}
The function works for NIGHT_NO
and NIGHT_YES
but not in FOLLOW_SYSTEM
It's not that I'm getting wrong values is that I received an exception
lateinit property captured has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property captured has not been initialized
at io.mockk.CapturingSlot.getCaptured(API.kt:2503)
But if I'll add any of the other tests first, it works
@Test
fun `test clicking on follow system radio button applies system theme`() {
initialize {
testTheme(MODE_NIGHT_YES, R.id.fragment_theme_selection_radio_button_dark)
testTheme(MODE_NIGHT_FOLLOW_SYSTEM, R.id.fragment_theme_selection_radio_button_system)
}
}
Now, I tried to debug it, I'm unable to even capture it