Skip to content
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

isEmpty() returns false on empty list #458

Closed
2 of 3 tasks
audunel opened this issue Jun 26, 2020 · 4 comments
Closed
2 of 3 tasks

isEmpty() returns false on empty list #458

audunel opened this issue Jun 26, 2020 · 4 comments

Comments

@audunel
Copy link

audunel commented Jun 26, 2020

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version (I am using springmockk 2.1.0, which in turn uses mockk 1.9.3)
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

I am mocking a service which returns a list, and is supposed to return an empty list

val connector = mockk<Connector>()
val service = Service(connector)

@Test(expect = CustomException)
fun shouldThrowException() {
    every { connector.getList() } returns emptyList()
    service.foo()

Where foo() is defined as

foo() {
    val returnedList = connector.getList()
    if(returnedList.isEmpty()) {
        throw CustomException()
    }
    ...
}

Current Behavior

The test fails as returnedList.isEmpty() returns false

Failure Information (for bugs)

I have examined the returnedList val, and it has size = 0.

Steps to Reproduce

  1. Set up a test where a mock service returns an empty list.
  2. Run isEmpty() on the returned list.

Context

  • MockK version: springmockk 2.0.1 (uses mockk 1.9.3)
  • OS: macOS Mojave
  • Kotlin version: 1.3.72
  • JDK version: 1.8.0_172
  • JUnit version: 4.13
  • Type of test: unit test
14:35:44.849 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Service1 name=#1
14:35:45.482 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Service2 name=#2
14:35:45.588 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Service3 name=#3
14:35:45.643 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Service4 name=#4
14:35:47.051 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for CustomClass name=child of #1#5
14:35:47.209 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for List name=child of #2#6
14:35:47.213 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering ***@5754de72 on Service1(#1).***(***@5d625f21)
14:35:47.215 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for List name=child of #2#7
14:35:47.216 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering List(child of #2#7) on Service2(#2).***-zKJe0gY(24000022, 123)
14:35:47.451 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering false on List(child of #2#7).isEmpty()
14:35:47.461 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for List name=child of #3#8
14:35:47.462 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering List(child of #3#8) on Service3(#3).***(***)
14:35:47.467 [main] DEBUG io.mockk.impl.instantiation.AbstractMockFactory - Creating mockk for Iterator name=child^2 of #3#8#9
14:35:47.502 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering Iterator(child^2 of #3#8#9) on List(child of #3#8).iterator()
14:35:47.510 [main] DEBUG io.mockk.impl.recording.states.AnsweringState - Answering false on Iterator(child^2 of #3#8#9).hasNext()

java.lang.AssertionError: Expected exception: 
org.example.project.exceptions.CustomException
@fabiobauer
Copy link

Hey @audunel,

can you reproduce this issue while using MockK (1.9.3) only? I cannot see an error here, It looks like this issue belongs to springmockk.

class Connector {
    fun getList(): List<String> {
        return listOf("foo")
    }
}

class Service(private val connector: Connector) {
    fun foo() {
        val returnedList = connector.getList()
        if(returnedList.isEmpty()) {
            throw RuntimeException()
        }
    }
}

class Test1 {
    @Test(expected = RuntimeException::class)
    fun shouldThrowException() {
        val connector = mockk<Connector>()
        val service = Service(connector)

        every { connector.getList() } returns emptyList()
        service.foo()
    }
}

@audunel
Copy link
Author

audunel commented Jun 29, 2020

Hi @fabiobauer

When running your example code, I was not able to reproduce the issue. It seems like there's some other issue with our code. Thank you for your time.

@audunel audunel closed this as completed Jun 29, 2020
@pikitgb
Copy link

pikitgb commented Mar 25, 2021

For others looking for answer:

I was having similar issue using mockk_version = '1.10.3-jdk8'
in my case was that i was returning a mockk relaxed instead the empty list

Instead of using this:

every {
   connector.getList()
} returns mockk(relaxed = true) { // this caused the issue for me returning isEmpty() => false
   emptyList<MyItem>()
}

Use this:

every {
   connector.getList()
} returns emptyList<MyItem>()

@Raibaz
Copy link
Collaborator

Raibaz commented Mar 25, 2021

This has also been fixed in more recent versions of mockk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants