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

Support new default value of spring boot 2.1 of spring.main.allow-bean-definition-overriding #17

Closed
huehnerlady opened this issue Nov 19, 2018 · 16 comments

Comments

@huehnerlady
Copy link

Hi,

will you add support of the fact that spring.main.allow-bean-definition-overriding is now defaulted to false and therefore your annotation ends up in a org.springframework.beans.factory.support.BeanDefinitionOverrideException?

@huehnerlady
Copy link
Author

I asked about a different annotation and they added information about it here:
spring-projects/spring-boot#15216 (comment)

@pchudzik
Copy link
Owner

Hi. I'll take a look into this but cannot promise when it will be fixed. To be honest, I've not used this library for some time now and I'm not working with spring-boot nor springboot2 at that moment, therefore, I'm not actively using this library now.

PR will be always welcomed :)

I've just created a branch on which I plan to work on this and added sample which fails the build:
https://github.com/pchudzik/springmock/tree/springboot2
https://travis-ci.org/pchudzik/springmock/builds/460429005

@huehnerlady
Copy link
Author

Hi,

I am not sure we would use this library, but if/when I have a free moment I am happy to look at it :)

pchudzik added a commit that referenced this issue Nov 30, 2018
pchudzik added a commit that referenced this issue Nov 30, 2018
pchudzik added a commit that referenced this issue Nov 30, 2018
pchudzik added a commit that referenced this issue Nov 30, 2018
@pchudzik
Copy link
Owner

@huehnerlady You can give it a try. It's been deployed to maven snapshots repo. I'll release it in a day or two after I double check if everything is working as supposed.

You can configure use snapshots using this instruction: https://github.com/pchudzik/springmock#snapshots

@pchudzik
Copy link
Owner

pchudzik commented Dec 3, 2018

I've just released it you can give it a try:

<dependency>
  <groupId>com.pchudzik.springmock</groupId>
  <artifactId>springmock-spock</artifactId>
  <version>1.2.0</version>
</dependency>

or

testCompile('com.pchudzik.springmock:springmock-spock:1.2.0')

@pchudzik pchudzik closed this as completed Dec 3, 2018
@huehnerlady
Copy link
Author

huehnerlady commented Jan 18, 2019

Hi,

Sorry that I didn't answer earlier.
Thank you for making the effort in fixing this.
Unfortunately I receive an Exception if I use @AutowiredMock now:

package my.package

import com.pchudzik.springmock.infrastructure.annotation.AutowiredMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.stereotype.Service
import spock.lang.Specification

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT

@SpringBootTest(classes = [TestApplication], webEnvironment = RANDOM_PORT)
class IntegrationTest extends Specification {

  @Service
  class MyService {

    String helloworld() {
      "Hello world"
    }
  }
  @AutowiredMock
  MyService myService

  def "should change return"() {
    given:
    myService.helloworld() >> "HELLOOOOOO"

    when:
    String actual = myService.helloworld()

    then:
    actual == "HELLOOOOOO"
  }
}

TestApplication:

package my.package

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.retry.annotation.EnableRetry
import org.springframework.scheduling.annotation.EnableAsync

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
@EnableRetry(proxyTargetClass = true)
class TestApplication {

  static void main(String[] args) {
    SpringApplication.run TestApplication, args
  }
}

When I run the test, I receive

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myService': Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [java.lang.Object]: 
Factory method 'createMock' threw exception; nested exception is java.lang.IllegalArgumentException: 
Could not create type

do you have any idea what I am doing wrong?

spring version: 5.1.2.RELEASE
spring boot: 2.1.0.RELEASE
springmock-spock: 1.2.0

Many thanks

@pchudzik
Copy link
Owner

pchudzik commented Jan 21, 2019

Hi. I'm not sure what you want to achieve.

You are defining class MyService (which is not even static so probably will not be picked up by spring) inside test class and want to mock it? It is not real life example is it?

Please take a look at:
Springboot2Application.java
AddOneTranslator.java
TwoRepository.java
MyService.java
SpockSamplesApplicationTest.groovy

It's a bit more complex and shows more examples of what can you do but in some way is very similar. It mocks behaviour of some service (AddOneTranslator), verifies interaction with other (spied TwoRepository) and shows mixed spring-mock with native spring mocks (LogService)

You can also find some more description in my blogpost

@huehnerlady
Copy link
Author

Well basically I had the problem in our system and so I tried to make a small example which leads up to that problem.

I have a Service (marked with the annotation @Servioce) and I have a test. in that test I want to mock the Service. This happens usually our Controller Integration tests where you tests errors etc. of the endpoint and one scenario would be that the service is called with the correct data.

But when I use the @AutowiredMock Annotation, I get the error shown above, that the method createMock could not create that type.

We do not have Mockito at all in our dependencies as we use spock for everything.
So any idea of why this problem occurs?

@pchudzik
Copy link
Owner

pchudzik commented Jan 22, 2019

Maybe you can implement this example inside springmock samples. Just clone the repo (git@github.com:pchudzik/springmock.git) there is project already configured with springbok2 in folder samples/spock-springboot2 you can import it in idea or eclipse and create classes that reflect how you are working with spring and springmock and the problem you have (from what you are saying it doesn't look like a lot of work and it will be much easier for me to debug it and see what's going on). Once you'll reproduce issue you have just create pull request for springmock (samples folder is also tested) and from there I'll take over and see what needs to be fixed.

If you could reproduce the issue you are having in there I'll be happy to help and fix any bug. From the stacktrace you have pasted I will not be able to help you out there is just not enough information for me to figure out what might be wrong.

@huehnerlady
Copy link
Author

Hi, that sounds fair, so I will see when I can implement it :)

@huehnerlady
Copy link
Author

I tried to reproduce it in your project, but could not achieve it. So for your project it seems to work.
As I do not have enough time to figure out why it is working at yours but not at ours at the moment I guess I have to stick with the property for now.

But thank you very much for your help.

@pchudzik
Copy link
Owner

Sure no problem.

You can also tray and provide more detailed stack trace and exact dependencies versions you are using? Sample project is simple so it might not necessary reflect real life case. Maybe there is something related to particular version of libraries you are using (for example cglib)

@huehnerlady
Copy link
Author

Aaah good point,
I will try to remove all company data from the project I used as an example and will add it here as an attachment once done :)

@huehnerlady
Copy link
Author

Hi,

while I was removing the data I showed it to a colleague, and he found out that your version of bytebuddy is not compatible with groovy 2.5.5 which we use in our systems.
If I overwrite your version with version 1.9.8 I do get a passing test :)
So maybe you could look into supporting groovy 2.5.5?
I know now that I just need to add the higher version of bytebuddy to make it work, so that's enough for me to try it out in our projects.

Many thanks for your support :)

@pchudzik
Copy link
Owner

So for now you are good to go with just overriding bytebuddy version :)

btw. If you are using Spock>=1.2 there is@SpringBean and @SpringSpy implemented natively as a part of spock-spring module.
http://spockframework.org/spock/docs/1.3-RC1/release_notes.html#_1_2_2018_09_23

@huehnerlady
Copy link
Author

oh wow thank you so much, I actually did not know about this :)

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

2 participants