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

Allow to customize an existing application (for tests, mocking, etc.) #44

Closed
sdeleuze opened this issue Jun 8, 2018 · 9 comments
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@sdeleuze
Copy link
Collaborator

sdeleuze commented Jun 8, 2018

Maybe using https://github.com/mockk/mockk.

@sdeleuze sdeleuze added this to the 1.0.0.M1 milestone Jun 8, 2018
@sdeleuze sdeleuze self-assigned this Jun 8, 2018
@sdeleuze
Copy link
Collaborator Author

Since we are going to leverage Spring Boot, mockito-kotlin 2 is probably a better option.

@sbuettner
Copy link

sbuettner commented Jun 21, 2018

@sdeleuze do you know if there is a plan to allow the usage of spring tests @MockBean with mockk?

Or maybe thats not even needed if there was an equivalent DSL for mocking beans in tests like

fun beforeAll() {
  app.run(profiles = "test").beans {
    mock<Foo>() 
    mock<Bar>().every { ... }
  }
}

Updated: Just saw you posted another comment (while I was writing this) regarding mockito-kotlin 2 which would work with @MockBean I guess. Nontheless a programmatic approach to mock beans could be a nice addition to avoid annotations.

@oleksiyp
Copy link

Added a ticket mockk/mockk#118

@sdeleuze sdeleuze modified the milestones: 0.0.1, 0.0.2 Aug 29, 2018
@sdeleuze sdeleuze modified the milestones: 0.0.2, 0.0.3 Oct 1, 2018
@sdeleuze sdeleuze changed the title Add mocking support Provide testing guidelines for each kind of tests (integration, mock, unit, sliced) Oct 1, 2018
@sdeleuze sdeleuze modified the milestones: 0.0.4, 0.0.5 Jan 2, 2019
@sdeleuze sdeleuze changed the title Provide testing guidelines for each kind of tests (integration, mock, unit, sliced) Provide mocking support Feb 1, 2019
@sdeleuze
Copy link
Collaborator Author

sdeleuze commented Feb 1, 2019

I finally had some times to play with Mockk, and it is now clear to me that it is what we should promote on Spring side to Mock beans in Spring applications written in Kotlin. @jnizet has created a nice @MockBean like support for Mockk with its springmockk, it will not be integrated for now in Spring Boot even if there is a big demand for that because current infra is sadly Mockito specific. But we can move forward on Spring Fu side by thinking what is missing for such first class support.

I think @sbuettner proposal about allowing to customize an existing application is interesting, and would be useful for other use case than mocking. We could maybe support something like as a first step:

fun beforeAll() {
  app.beans {
    bean { mock<Foo>(isPrimary = true)  }
    bean { mock<Bar>(isPrimary = true).every { ... } }
  }.run(profiles = "test")
}

Then as a second step (with another dedicated issue) we could provide a functional Mockk equivalent to @MockBean like beans { mockBean<Foo>() }.

Any thoughts?

@oleksiyp
Copy link

oleksiyp commented Feb 1, 2019

Syntax looks good to me. Mockk was designed to have few front facing DSLs via so called Gateway API

@sdeleuze sdeleuze changed the title Provide mocking support Allow to customize an existing application (for tests, mocking, etc.) Feb 1, 2019
@sdeleuze
Copy link
Collaborator Author

sdeleuze commented Feb 1, 2019

It turned out to be quite easy and powerful since we now can customize an existing app with any DSL in an additive configuration mode, only beans { } have the isPrimary = true capability for now.

val app = application {
    beans {
        bean { Bar("original") }
    }
}
app.customize {
    beans {
        bean(isPrimary = true) {
            mockk<Bar> {
                every { value } returns "customized"
            }
        }
    }
}

@sdeleuze
Copy link
Collaborator Author

sdeleuze commented Feb 1, 2019

I have created #151 for a Kofu equivalent of @MockkBean.

@sbuettner
Copy link

@sdeleuze I like your approach as it uses the default infra to register mocks.

I could still see an extension method inside the beans {} context like mockk<Foo> {...} that internally calls bean(isPrimary = true) as this seems the be the main usage. A scenario where a mocked bean should not be registered as the primary one seems to be exception. As isPrimary in this case would have the default value of true one can always override it with false.

@sdeleuze
Copy link
Collaborator Author

sdeleuze commented Feb 4, 2019

Since for now we recreate context every time with no specific caching involved, we can indeed maybe provide a simple mockkBean alias without having to invloved all the @MockkBean infrastructure. Let's discuss that as part of #151.

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

No branches or pull requests

3 participants