spring-projects / spring-projects/spring-hateoas
Integration test fails when using @MockBean which is type ObjectMapper
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 476
- PR merge metrics
- No merged PRs in 30d
Description
I have an integration test as per the screenshot below where I am trying to mock the methods for ObjectMapper.
After implementing spring hateos this no longer works and returns an error. I've tried changing this to a @SpyBean but this didn't work either. The only workaround I can think of is creating a test configuration and passing the object mapper (mocked) to the class being tested which would require overriding the bean which I really don't want to do as it looks messy.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hypermediaWebMvcConverters' defined in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.hateoas.config.WebConverters]: Factory method 'hypermediaWebMvcConverters' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1290)
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getObject(DefaultListableBeanFactory.java:1884)
at org.springframework.hateoas.config.RestTemplateHateoasConfiguration$HypermediaRestTemplateBeanPostProcessor.postProcessBeforeInitialization(RestTemplateHateoasConfiguration.java:68)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
... 133 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.hateoas.config.WebConverters]: Factory method 'hypermediaWebMvcConverters' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 149 more
Caused by: java.lang.NullPointerException
at org.springframework.hateoas.mediatype.hal.HalMediaTypeConfiguration.configureObjectMapper(HalMediaTypeConfiguration.java:84)
at org.springframework.hateoas.config.WebConverters.lambda$new$0(WebConverters.java:50)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.springframework.hateoas.config.WebConverters.<init>(WebConverters.java:51)
at org.springframework.hateoas.config.WebConverters.of(WebConverters.java:67)
at org.springframework.hateoas.config.HateoasConfiguration.hypermediaWebMvcConverters(HateoasConfiguration.java:79)
at org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$88358704.CGLIB$hypermediaWebMvcConverters$1(<generated>)
at org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$88358704$$FastClassBySpringCGLIB$$9d36ce51.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$88358704.hypermediaWebMvcConverters(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 150 more

Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the integration test shown in the issue, then inspect HalMediaTypeConfiguration.java, WebConverters.java, and HateoasConfiguration.java at the stack-trace locations. Determine why an @MockBean ObjectMapper causes hypermediaWebMvcConverters to fail, and add a regression test showing that the integration context starts successfully with the mock.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100