适用于 Azure 服务总线的 Spring Cloud Stream Binder 破坏了测试

问题描述 投票:0回答:2

添加依赖项

        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId>
        </dependency>

突然使我的 RestTemplate 客户端测试的 some 失败,并出现以下错误:

java.lang.AssertionError: JSON path "$"
Expected: (a collection containing <152> and a collection containing <153>)
     but: a collection containing <152> was String "<UnmodifiableRandomAccessList><item>152</item><item>153</item></UnmodifiableRandomAccessList>"

Expected :application/json
Actual   :application/xml;charset=UTF-8

我的测试如下:

    @Autowired
    private RestTemplate restTemplate;
    
    private MockRestServiceServer mockServer;
    
    @BeforeEach
    public void init()
    {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    public void test() throws Exception
    {
        mockServer.expect(requestTo(url))
            .andExpect(method(HttpMethod.POST))
            .andExpect(jsonPath("$", hasItems(152, 153)))
            .andRespond(withStatus(HttpStatus.OK)
            .contentType(MediaType.APPLICATION_JSON)
            .body("{}")); // actual code removed for brevity
        
        ResultDTO result = client.makeRestCall(Set.of(1L)); // beneath makes a RestTemplate call to the url

        assertNotNull(result);
    }

奇怪的是,测试类中只有部分测试失败,即该类有 8 个测试,其中 4 个测试失败,且没有明显的原因。我看到的是,我删除了 Azure 依赖项,测试通过了。有谁知道这种依赖会带来什么可能破坏我的代码?

java spring-boot azureservicebus spring-cloud-stream spring-test
2个回答
1
投票

似乎这是已知问题。问题在于 Azure 依赖项引入了

jackson-dataformat-xml
依赖项,它破坏了其余控制器响应的格式(使它们成为 XML 而不是默认的 JSON)。解决该问题的简单方法是排除依赖项:

<exclusion>
    <artifactId>jackson-dataformat-xml</artifactId>              
    <groupId>com.fasterxml.jackson.dataformat</groupId>
</exclusion>

该线程提到依赖项已变为可选,但我找不到发生这种情况的版本。


0
投票

您必须通过添加扩展来使您的 spring-cloud-azure-stream-binder-servicebus 依赖项如下:

<dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId>
        
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
            </exclusion>
        </exclusions>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.