Comments (16)
I asked about a different annotation and they added information about it here:
spring-projects/spring-boot#15216 (comment)
from springmock.
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
from springmock.
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 :)
from springmock.
@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
from springmock.
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')
from springmock.
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
from springmock.
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
from springmock.
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?
from springmock.
Maybe you can implement this example inside springmock samples. Just clone the repo ([email protected]: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.
from springmock.
Hi, that sounds fair, so I will see when I can implement it :)
from springmock.
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.
from springmock.
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)
from springmock.
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 :)
from springmock.
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 :)
from springmock.
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
from springmock.
oh wow thank you so much, I actually did not know about this :)
from springmock.
Related Issues (17)
- Figure out unified testing infrastructure/interfaces to ensure that mocking library works HOT 1
- Determine beanName just like what `@MockBean` do HOT 4
- Non-spock tests failing with IllegalArgumentException HOT 3
- mockito spies are not resetted HOT 1
- Check if it's working with spring5 HOT 1
- check if it works with spring data repositories HOT 1
- Null Pointer exception when names don't match up HOT 2
- Allow to leave spring proxy as it is when creating spies HOT 2
- Add support for different groovy versions when using spock HOT 1
- allow to create mocks without injecting them into specification HOT 2
- add support for additional configuration HOT 1
- support for java7 HOT 1
- configure automated deployment HOT 1
- Question about WebMvcTest support HOT 7
- spy should be distinguished from mock if they are both of the same class HOT 3
- inject mocks by name HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from springmock.