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

Mocking concrete classes using spring and detachedMockFactory #759

Closed
hm1rafael opened this Issue Aug 22, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@hm1rafael

hm1rafael commented Aug 22, 2017

When creating mocks of concrete classes using DetachedMockFactory and annotating a method with @Bean if the concrete class has other spring dependencies (@Autowired dependencies, @PostConstruct), should a Mock try to inject dependencies since will be used to "emulate" interactions?

I'm using Intellij,
Groovy: 2.4.10
Java: 1.8
OS: Ubuntu 17.04
Spock: 1.1-groovy2.4

@leonard84

This comment has been minimized.

Show comment
Hide comment
@leonard84

leonard84 Aug 22, 2017

Member

@hm1rafael I don't understand what you mean, please elaborate further and provide a SSCCE

Member

leonard84 commented Aug 22, 2017

@hm1rafael I don't understand what you mean, please elaborate further and provide a SSCCE

@hm1rafael

This comment has been minimized.

Show comment
Hide comment
@hm1rafael

hm1rafael Aug 23, 2017

Hi @leonard84,

What I meant was, if you create a mock as a spring bean using DetachedMockFactory, if the class being mocked is a concrete class and has other spring dependencies, the Bean creation fails.

I created a simple example comparing what happens with spock mock creation and spring boot/mockito mocks, let me know if it is clear now.
Example: https://github.com/hm1rafael/mock-spock-spring

hm1rafael commented Aug 23, 2017

Hi @leonard84,

What I meant was, if you create a mock as a spring bean using DetachedMockFactory, if the class being mocked is a concrete class and has other spring dependencies, the Bean creation fails.

I created a simple example comparing what happens with spock mock creation and spring boot/mockito mocks, let me know if it is clear now.
Example: https://github.com/hm1rafael/mock-spock-spring

@leonard84

This comment has been minimized.

Show comment
Hide comment
@leonard84

leonard84 Aug 28, 2017

Member

If you want this behavior, then you need to use a different way to register those beans (credits to @twicksell).

class MockPostProcessor implements BeanFactoryPostProcessor {

    def detachedMockFactory = new DetachedMockFactory()

    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.registerSingleton("example", detachedMockFactory.Mock(Example))
    }
}

You can use it like this:

class ExampleSpockTestConfiguration {
    def detachedMockFactory = new DetachedMockFactory()

    @Bean
    MockPostProcessor mockPostProcessor() {
        new MockPostProcessor(['example': detachedMockFactory.Mock(Example)])
    }

    static class MockPostProcessor implements BeanFactoryPostProcessor {
        private final Map<String, Object> mocks

        MockPostProcessor(Map<String, Object> mocks) {
            this.mocks = mocks
        }

        void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            mocks.each { name, mock ->
                beanFactory.registerSingleton(name, mock)
            }
        }
    }
}
Member

leonard84 commented Aug 28, 2017

If you want this behavior, then you need to use a different way to register those beans (credits to @twicksell).

class MockPostProcessor implements BeanFactoryPostProcessor {

    def detachedMockFactory = new DetachedMockFactory()

    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.registerSingleton("example", detachedMockFactory.Mock(Example))
    }
}

You can use it like this:

class ExampleSpockTestConfiguration {
    def detachedMockFactory = new DetachedMockFactory()

    @Bean
    MockPostProcessor mockPostProcessor() {
        new MockPostProcessor(['example': detachedMockFactory.Mock(Example)])
    }

    static class MockPostProcessor implements BeanFactoryPostProcessor {
        private final Map<String, Object> mocks

        MockPostProcessor(Map<String, Object> mocks) {
            this.mocks = mocks
        }

        void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            mocks.each { name, mock ->
                beanFactory.registerSingleton(name, mock)
            }
        }
    }
}
@leonard84

This comment has been minimized.

Show comment
Hide comment
@leonard84

leonard84 Jul 7, 2018

Member

With #796 we've added an alternative will help in this case.

Member

leonard84 commented Jul 7, 2018

With #796 we've added an alternative will help in this case.

@leonard84 leonard84 closed this Jul 7, 2018

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