pchudzik / springmock Goto Github PK
View Code? Open in Web Editor NEWalternative spring mocking infrastructure
Home Page: https://blog.pchudzik.com/201707/springmock-v1/
License: MIT License
alternative spring mocking infrastructure
Home Page: https://blog.pchudzik.com/201707/springmock-v1/
License: MIT License
For example reset mode for mockito beans, and stuff like that.
A lot of code is written in java8 because it was the easiest one. Maybe support for java7 will be good idea?
see test case com.pchudzik.springmock.infrastructure.test.name.DoubleNameShouldBeUsedAsQualifier
it should be possible to inject different mocks without the @qualifier annotation
currently it fails with:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.pchudzik.springmock.infrastructure.test.name.DoubleNameShouldBeUsedAsQualifier$MockService' available: expected single matching bean but found 2: mock1,mock2
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
?
Do deployment on every commit (snapshot) using travis infrastructure
Spring Boot's @MockBean
determines beanName automatically:
private String determineBeanName(String[] existingBeans, SpyDefinition definition,
BeanDefinitionRegistry registry) {
if (StringUtils.hasText(definition.getName())) {
return definition.getName();
}
if (existingBeans.length == 1) {
return existingBeans[0];
}
return determinePrimaryCandidate(registry, existingBeans,
definition.getTypeToSpy());
}
So that the mock will automatically override existing beans without raising NoUniqueBeanDefinitionException
like:
No qualifying bean of type 'com.cht.commons.security.login.UserRegistry' available: expected single matching bean but found 2: userRegistry,userRegistryMock
Hi Paweł,
Great library. I'm going to use it for mocking dependencies for Spring MVC controller tests.
Did you try it with @ WebMvcTest ?
@WebMvcTest(MyController)
class MyControllerTest extends Specification {
@Autowired
MockMvc mockMvc
@AutowiredMock
MyService myService
}
now i'm having exception:
java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
at com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener.tryToGetBean(MockAttachingTestExecutionListener.java:122)
at com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener.beforeTestMethod(MockAttachingTestExecutionListener.java:62)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
Do I understand correctly that in current version(1.0.0) it is not supported?
If the mocks variable name doesn't match the bean name, it can't find the bean to mock, and it falls over with a NPE. I can get round it by making the names match, but seems a bit inflexible.
for example:
@AutowiredSpy ProjectionService spy
java.lang.NullPointerException
at org.spockframework.mock.MockUtil.asMock(MockUtil.java:54)
at org.spockframework.mock.MockUtil.attachMock(MockUtil.java:68)
at com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener.beforeTestMethod(MockAttachingTestExecutionListener.java:59)
My project has a large mix of Spock and Junit tests, the spock tests use a mix of Mockito and Spock mocks, the JUnit tests just use Mockito, since adding this library all of my non spock tests fail with the following error:
Stacktrace
java.lang.IllegalArgumentException: MockAttachingTestExecutionListener can be applied only for spock specifications
at com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener.beforeTestMethod(MockAttachingTestExecutionListener.java:48)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
Is there anyway to globally turn the listener off, and only add it to the tests where I'm making use of the library? (until such time as I can actually convert the whole code base)
see
com.pchudzik.springmock.mockito.spring.MockitoMockResetTestExecutionListener
com.pchudzik.springmock.mockito.spring.MockitoMockResetTestExecutionListenerTest
mocks are handled properly but spies are completely ignored but should be handled in the same way as mocks
If mock is of the same class as spy and both are injected into specification infrastructure doesn't distinguish them and injects mock in place of spy (or other way around).
Sample test case:
'''
@AutowiredSpy
MyService spy;
@AutowiredMock
MyService mock;
@Test
public void spy_should_be_distinguishable_from_mock_of_the_same_class() {
assertNotSame(spy, mock);
}
@Test
public void should_distinguish_mock() {
assertSame(aMock, mock);
}
@Test
public void should_distinguish_spy() {
assertSame(aSpy, spy);
}
'''
Detailed test case: com.pchudzik.springmock.infrastructure.test.SpyAndMockOfTheSameClassShouldBeDistinguishable (ignored right now)
Check if it's working with spring5
Like with @MockBean from spring:
@Configuration
@MockBean(Service.class)
static class Config {}
Will create bean in the scope but will not force you to inject it into specification
Tests for mockito and spock are basically the same. Internal implementation might be different but requirements are the same for both of those libraries.
Figure out how to create unified interfaces and ensure that all tests cases are covered in both.
check if it works with spring data repositories
spock1.1 is required but groovy version isn't fixed.
Add support for different groovy versions (2.3, 2.4, 2.0) the same versions spock does support - http://mvnrepository.com/artifact/org.spockframework/spock-core
In case of asynchronous code @async you might want to leave default behavior and do not unwrap created proxy.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.