契约提供者测试已损坏:pactVerificationTestTemplate»PreconditionViolation

时间:2018-09-17 00:49:44

标签: junit5 pact pact-jvm pact-broker

我对CDC测试非常陌生,只是迈出了第一步。我已经部署了Pact-Broker(docker-compose),并在localhost:80运行。消费者将生成的契约成功地发送给了经纪人,但是提供者似乎无法获得有效的合同(但这只是假设)。

我正在使用spring-boot,maven,jUnit5。应用程序测试在Ubuntu上运行。 在本地目录中将PactFolder与消费者生成的pact-contracts一起使用可成功进行测试。

当我切换到@PactBroker批注时,提供程序能够连接到代理,并且它收到以下响应(我是从调试日志中获得的):

{"_links":
    {"self":{
        "href":"http://localhost/pacts/provider/provider-    name/latest","title":"Latest pact versions for the provider provider-name"},
    "pb:provider":{"href":"http://localhost/pacticipants/provider-name",
        "name":"provider-name"},
    "pb:pacts":[
        {"href":"http://localhost/pacts/provider/provider-name/consumer/consumer-name/version/1.0.0",
        "title":"Pact between consumer-name (v1.0.0) and provider-name",
        "name":"consumer-name"} 
     ],
    "provider":{
        "href":"http://localhost/pacticipants/provider-name",
        "title":"provider-name",
        "name":"DEPRECATED - please use the pb:provider relation"
     },
    "pacts":[
            {"href":"http://localhost/pacts/provider/provider-name/consumer/consumer-name/version/1.0.0",
            "title":"DEPRECATED - please use the pb:pacts relation. Pact between consumer-name (v1.0.0) and provider-name",
            "name":"consumer-name"
            }
        ]
    }
}

测试运行结果如下:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.758 s 
FAILURE! - in com.tis.payment.mapper.PaymentMapperApplicationTests
[ERROR] pactVerificationTestTemplate{PactVerificationContext}  
Time elapsed: 9.752 s  
ERROR!
org.junit.platform.commons.util.PreconditionViolationException: 
No supporting TestTemplateInvocationContextProvider provided an invocation context
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   PaymentMapperApplicationTests.pactVerificationTestTemplate » PreconditionViolation
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

由于使用本地pact文件会使测试变为绿色,因此我认为原因不在我的测试类的代码中,尽管如果有帮助的话,请在此处提供它:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
        properties = "server.port=8082")
@Provider("provider-name")
@PactBroker(host = "localhost", port = "80", tags="latest")
//@PactFolder("target/pacts") # uncomment to use local pact files
public class ApplicationTests {

    @MockBean
    private ProviderServiceClient providerServiceClient;

    @BeforeEach
    void setupTestTarget(PactVerificationContext context) {
        context.setTarget(new HttpTestTarget("localhost", 8082, "/"));
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @State({"valid payment file"})
    public void toValid() {
        ServiceResponse response = new ServiceResponse();
        response.setBatchId("test");
        response.setId(1L);
        when(providerServiceClient.save(any())).thenReturn(response);
    }

    @State({"invalid payment file"})
    public void toInvalid() {
    }
}

由于无法使用本地协议文件,我真的很想知道如何解决该错误,并感谢您提供任何有用的意见

Maven协定依赖项:

<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-model</artifactId>
    <version>3.5.22</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-junit5_2.12</artifactId>
    <version>3.5.22</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-consumer-junit5_2.12</artifactId>
    <version>3.5.22</version>
    <scope>test</scope>
</dependency>

要让Maven发布消费者的契约,请使用以下插件:

<plugin>
    <groupId>au.com.dius</groupId>
    <artifactId>pact-jvm-provider-maven_2.12</artifactId>
    <version>3.5.22</version>
    <configuration>
        <pactBrokerUrl>http://localhost:80</pactBrokerUrl>
        <trimSnapshot>true</trimSnapshot>
        <!-- Defaults to false -->
    </configuration>
</plugin>

pact-provider docker-compose.yml:

version: '2'
    services:
        postgres:
          image: postgres
          restart: always
          # healthcheck:
            # test: psql postgres --command "select 1" -U postgres
          ports:
            - "5432:5432"
          environment:
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: password
            POSTGRES_DB: postgres
        broker_app:
          image: dius/pact-broker
          depends_on: 
            - postgres
          ports:
            - "80:80"
          links:
            - postgres
          environment:
            PACT_BROKER_DATABASE_USERNAME: postgres
            PACT_BROKER_DATABASE_PASSWORD: password
            PACT_BROKER_DATABASE_HOST: postgres
            PACT_BROKER_DATABASE_NAME: postgres

1 个答案:

答案 0 :(得分:1)

JUnit 5错误org.junit.platform.commons.util.PreconditionViolationException: No supporting TestTemplateInvocationContextProvider provided an invocation context表示未提供测试上下文,因此无法调用模板化的测试方法。这可能是由于没有要验证的协定(每个协定都会在调用上下文中导致)。

现在要解决实际问题,即为什么您没有从经纪人那里得到任何要验证的协议。 Pact Broker本质上是一个存储库,JUnit 5验证框架将使用pact类上的所有注释来创建要发送到Pact Broker的查询。该查询未返回任何协定,因此某处必须不匹配。

从您提供的信息中,我唯一看到的是JSON中的URL "http://localhost/pacts/provider/provider- name/latest"有问题(提供程序名称中存在空格)。如果那不仅仅是SO的格式问题,那将是不匹配的(经纪人可能会返回带有该URL的404)。

如果这不是问题,请检查从Maven运行验证时是否可以使用与测试框架相同的方式访问代理。启用DEBUG级别日志记录将显示所有正在发出的请求。使用curl之类的东西,然后尝试相同的请求,看看会得到什么。