Spring-boot启动程序中的Pact实现

时间:2017-08-09 07:46:44

标签: java spring maven spring-boot spring-boot-starter

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

以下是AutoConfigure实现:

@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 without mockServer which is null`

@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 repository的链接。所有的帮助将不胜感激。

非常感谢!

0 个答案:

没有答案