1

我已经开始尝试将 pact-framework 实现到 spring-boot-starter 中,但我偶然发现了一个问题。我希望可以通过 application.properties 配置消费者、生产者等。目前我已经创建了三个模块AutoConfigureStarterSample。在示例中,我试图实现一个消费者。当我在里面寻找 dependency:tree 时,Starter我可以看到它们。

这是自动配置的实现:

@Configuration
@ConditionalOnBean({PactProviderRuleMk2.class})
@EnableConfigurationProperties(PactProperties.class)
public class PactAutoConfiguration {

private static Log log = LogFactory.getLog(PactAutoConfiguration.class);

@Autowired
private PactProperties properties;

@Bean
@ConditionalOnMissingBean
public PactProviderRuleMk2 pactProviderRuleMk2() {
        if (this.properties.getProvider().getName() == null
            || this.properties.getProvider().getHost() == null
            || this.properties.getProvider().getPort() == 0)
        {
            String msg = "PactProviderRuleMk2 properties not configured properly." +
                " Please check test.pact.provider.* properties settings in configuration file.";
            log.error(msg);
            throw new RuntimeException(msg);
        }

        PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2(
                this.properties.getProvider().getName(),
                this.properties.getProvider().getHost(),
                this.properties.getProvider().getPort(),
                this);

        return mockProvider;
    }
}

当我调试时,ConsumerPortTest我得到了rule初始化,但没有pacts (which is size=0) and withoutmockServer which isnull`

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
@WebAppConfiguration
public class ConsumerPortTest {

    @Rule
    @Autowired
    // public PactProviderRuleMk2 rule = new PactProviderRuleMk2("Foo_Provider", "localhost", 8080, this);
    public PactProviderRuleMk2 rule;

    @Pact(provider="Foo_Provider", consumer="Foo_Consumer")
    public RequestResponsePact createFragment(PactDslWithProvider builder) {
        return builder.uponReceiving("a request for Foos")
                .path("/foos")
                    .method("GET")
                    .willRespondWith()
                    .status(200)
                    .body("[{\"value\":42}, {\"value\":100}]")
                .toPact();
    }

    @Test
    @PactVerification("Foo_Provider")
    public void runTest() {
        ConsumerPort consumerPort = new ConsumerPort(rule.getConfig().url());
        assertThat("Response JSON body has to have values 42 and 100!",
                consumerPort.foos(),
                is(Arrays.asList(new Foo(42), new Foo(100))));
    }
}

这是我提交的 GitHub存储库的链接。所有的帮助将不胜感激。

非常感谢!

4

0 回答 0