The mock server doesn't seem to run even if I have the PactProviderRule

2,271 views
Skip to first unread message

Naren Nala

unread,
May 15, 2016, 10:11:50 PM5/15/16
to Pact
I continuously have the client get call that I am trying fail with a Connection refused. If I manually start listening on localhost and the said port, my client works fine. So, I am guessing the mock server is not starting/not binding to the specified ip and port. Any help is greatly appreciated. Thanks in advance. My complete test class -

public class CachingConsumerTest {

@Rule
public PactProviderRule mockTestProvider = new PactProviderRule("test_provider", "localhost", 8080, this);

@Pact(provider = "test_provider", consumer = "test_consumer")
protected PactFragment createFragment(PactDslWithProvider builder) {

return builder
.uponReceiving("a request for something")
.path("/v1/event-publish/node-topics")
.method("GET")
.willRespondWith()
.status(200)
.body("{\"responsetest\":true}").toFragment();
}

@Test
@PactVerification("test_provider")
public void runTest() throws IOException {
EventPublisherClientConfiguration clientConfiguration = new EventPublisherClientConfiguration();
clientConfiguration.setBaseUrl("http://localhost:8080");
clientConfiguration.setTimeout(Duration.seconds(30));
EventPublisherClient client = new EventPublisherClient(clientConfiguration);
assertEquals(client.build().findAll(""), "{\"responsetest\":true}");
}
}

Naren Nala

unread,
May 16, 2016, 2:41:18 PM5/16/16
to Pact
Never mind. I got it fixed myself. The pact fragments weren't getting generated properly in the createFragments() method of PactProviderRule. Updated the createFragment() method to pubic and the issue resolved itself. Thanks!

Ronald Holshausen

unread,
May 16, 2016, 6:05:20 PM5/16/16
to Naren Nala, Pact
I'm glad you got it sorted out. I'll look to update the PactProviderRule to give an indication if the access rights are not correct.

--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

Shiling Hu

unread,
May 30, 2016, 11:48:01 PM5/30/16
to Pact, naren...@gmail.com
I can execute ExampleJavaConsumerPactTest successfully but every other examples with PactProviderRule will fail. For example, I use the example test from https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-consumer-junit%2Fsrc%2Ftest%2Fjava%2Fau%2Fcom%2Fdius%2Fpact%2Fconsumer%2FPactRuleWithRandomPortTest.java, the createFragment() method is public. Below is the code.
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRule;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.PactFragment;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class PactRuleWithRandomPortTest {
Logger LOGGER = LoggerFactory.getLogger(PactRuleWithRandomPortTest.class);
    @Rule
    public PactProviderRule rule = new PactProviderRule("test_provider", this);

    @Pact(provider="test_provider", consumer="test_consumer")
    public PactFragment createFragment(PactDslWithProvider builder) {
        return builder
            .given("test state")
            .uponReceiving("random port test interaction")
                .path("/")
                .method("GET")
            .willRespondWith()
                .status(200)
                .body("{\"ok\": true}")
            .toFragment();
    }

    @Test
    @PactVerification("test_provider")
    public void runTest() throws IOException {
        Map expectedResponse = new HashMap();
        expectedResponse.put("ok", true);
        assertEquals(new ConsumerClient("http://localhost:" + rule.getConfig().port()).getAsMap("/", ""), expectedResponse);
    }
}

I will get the error msg as below. I use eclipse. I guess the mock server will start itself, and there is no need to add an apache server into eclipse, right? I have tried to add an apache to the eclipse and it fails too. And I don't know what has been wrong.




在 2016年5月17日星期二 UTC+8上午6:05:20,Ronald Holshausen写道:

Ronald Holshausen

unread,
May 31, 2016, 12:35:51 AM5/31/16
to Shiling Hu, Pact, Naren Nala
This works correctly when run from Intellij IDEA. Eclipse must be doing something when running the tests. You don't need to start anything, the mock server will start up correctly.

Make sure that Eclipse uses a version of JUnit that supports test rules (version 4.9+). It looks like the provider rule is not running, so the mock server is not being started.

Shiling Hu

unread,
May 31, 2016, 3:00:43 AM5/31/16
to Pact, joyces...@gmail.com, naren...@gmail.com
I change my junit version from 4.8 to 4.9, and it works! Thanks!

在 2016年5月31日星期二 UTC+8下午12:35:51,Ronald Holshausen写道:
Reply all
Reply to author
Forward
0 new messages