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

Missing Feature? Mocking dynamic type #31

Closed
a1flecke opened this issue Feb 21, 2018 · 9 comments
Closed

Missing Feature? Mocking dynamic type #31

a1flecke opened this issue Feb 21, 2018 · 9 comments

Comments

@a1flecke
Copy link

Use Case: In mockito I was doing this:

private void assertRequestClassWasUsed(final Class<? extends AbstractRequest> requestKlass, Runnable doIt, Consumer<QueueWriter> verification) {
        final QueueWriter queueWriter = expectedQueueWriter.get(requestKlass);
        assertThat(queueWriter).as("Could not find queuewriter for payload %s", requestKlass.getCanonicalName()).isNotNull();
        final AbstractRequest request = (AbstractRequest)mock(requestKlass);  //<-- This
        when(queueWriter.getName()).thenReturn(QUEUE_NAME);
        when(message.getPayload()).thenReturn(request);
        when(receivedMessage.getPayload()).thenReturn(request);
        doIt.run();
        verification.accept(queueWriter);
        Mockito.reset(primaryQueueWriter, refreshQueueWriter, harvestQueueWriter, createQueueWriter);

I cannot figure out a way in mockk to do the equivalent of mock(requestClass) where the class is not known at compile time. I fully admit that I am missing something here.

@oleksiyp
Copy link
Collaborator

This is to be added. I was thinking about use cases before and it seems for me pretty weird. For plugins/extensions and less "user-facing" things there is direct Gateway API that can be used.

@a1flecke
Copy link
Author

@oleksiyp Thanks.

@oleksiyp
Copy link
Collaborator

Gateway API should be okay if you do it once. Check #23

@oleksiyp
Copy link
Collaborator

I think I can easily add something alike classMockk, as your case is not extension or plugin. #23 was exactly about JUnit extension

oleksiyp added a commit that referenced this issue Feb 25, 2018
@oleksiyp
Copy link
Collaborator

oleksiyp commented Feb 25, 2018

@a1flecke please check http://mockk.io/#class-mock and close ticket if it fulfills your request. Vesrion 1.7.8

@a1flecke
Copy link
Author

a1flecke commented Feb 25, 2018 via email

@oleksiyp
Copy link
Collaborator

Hm, looks something is going wrong, but I don't understand why you require calling "reset".

It is not "reset the world" actually.

This reset is rather local for "every" and "verify" constructs. It is resetting state machine that controls recording state(i.e. one when calls are recorded for verification/stubbing) vs normal state(when it is just called). It is in public API because useful to be called for example before tests via some testing framework extensions to isolate tests.

Have you checked clearMocks function? Why is it not fitting your needs?

@a1flecke
Copy link
Author

a1flecke commented Feb 26, 2018

@oleksiyp

Have you checked clearMocks function? Why is it not fitting your needs?

I must have missed this. Switched to using this. Thanks.

@oleksiyp oleksiyp closed this as completed Mar 3, 2018
@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 3, 2018

Seems main issue was fixed

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

No branches or pull requests

2 participants