GithubHelp home page GithubHelp logo

pchudzik / springmock Goto Github PK

View Code? Open in Web Editor NEW
22.0 22.0 4.0 434 KB

alternative spring mocking infrastructure

Home Page: https://blog.pchudzik.com/201707/springmock-v1/

License: MIT License

Java 88.52% Shell 0.02% Groovy 11.47%
mock mocking mockito spock spring spring-boot spring-test

springmock's People

Contributors

pchudzik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

springmock's Issues

support for java7

A lot of code is written in java8 because it was the easiest one. Maybe support for java7 will be good idea?

inject mocks by name

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

Determine beanName just like what `@MockBean` do

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

Question about WebMvcTest support

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?

Null Pointer exception when names don't match up

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)

Non-spock tests failing with IllegalArgumentException

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)

mockito spies are not resetted

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

spy should be distinguished from mock if they are both of the same class

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.