Spring在生产和测试中生成不同的HATEOAS链接 到目前为止的调查

问题描述

在我的春季启动应用程序中,我正在做RepositoryEntityLinks#linkToItemResource创建一个hateoas链接,但是在生产和测试中得到不同的结果。

呼叫 注入的repositoryEntityLinks.linkToItemResource(New.class,item.getId())上的RepositoryEntityLinks导致

中的正常环境
                    "new": {
                        "href": "http://localhost:8080/api/news/13244{?projection}","templated": true
                    }

以及在我的集成测试中

                    "new": {
                        "href": "http://localhost:8080/api/new/13244"
                    }

特别是缺少的多元性破坏了我的考验。

示例测试:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,properties="spring.datasource.name=te2et")
@EnableAutoConfiguration(exclude = { ElasticSearchRestHealthContributorAutoConfiguration.class })
class TimelineEndpointE2ET extends E2EBaseTest {

    @MockBean
    NewRepository newRepository;

    @Test
    void test() {
        // my rest assured test
    }
}

(我已经发现,New可能是一个实体的坏名字,但我认为这并不是错)


到目前为止的调查

在生产中,我使用EvoInflectorTypeBasedCollectionResourceMapping负责实体名称的多元化。另一方面,测试使用MappingResourceMetadata

启动时,RepositoryResourceMappings#populateCache将为所有注册的存储库创建映射并使用EvoInflectorTypeBasedCollectionResourceMapping。 由于我对NewRepository的嘲笑,此存储库未被发现,因此不会放入该缓存中。

稍后,当调用RepositoryEntityLinks#linkToItemResource时,PersistentEntitiesResourceMappings将创建MappingResourceMetadata的映射(而不是EvoInflectorTypeBasedCollectionResourceMapping)。

问题是问自己:

  • 为什么未发现MockBean?
  • 为什么在启动时填充缓存以及以后进行临时添加时使用不同的ResourceMapping

我认为我在这里缺少很多重要信息,希望有人能指导我正确的方向。

谢谢!

我正在使用Spring Boot 2.2.4.RELEASE

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...