Wiremock port problem

5,301 views
Skip to first unread message

Aziz Ben Miled

unread,
Apr 23, 2018, 10:33:51 AM4/23/18
to wiremock-user
In short, I want to mock an API response with:

@Rule
    public WireMockRule wireMockRule = new WireMockRule();
    String wireMockPort = String.valueOf(wireMockRule.port());

And I get this exception every time:

java.lang.IllegalStateException: Not listening on HTTP port. The WireMock server is most likely stopped
    at com.google.common.base.Preconditions.checkState(Preconditions.java:444)
    at com.github.tomakehurst.wiremock.WireMockServer.port(WireMockServer.java:176)
    at com.rte.sdm.batch.job.ref.NebefRefOeItemReaderTest.<init>(NebefRefOeItemReaderTest.java:31)
    .....

Help please
 

Tom Akehurst

unread,
Apr 23, 2018, 10:35:21 AM4/23/18
to wiremock-user
The WireMock server isn't started at the point you're calling port(). Try adding an init method annotated with @Before and setting the port there instead.

Aziz Ben Miled

unread,
Apr 23, 2018, 10:46:12 AM4/23/18
to wiremock-user
any example to do this setting ?

Aziz Ben Miled

unread,
Apr 23, 2018, 10:51:33 AM4/23/18
to wiremock-user
i tried
@Before
    public void init() {
        WireMock.configureFor(wireMockRule.port());
    }
but the result is the same.

Tom Akehurst

unread,
Apr 23, 2018, 10:54:05 AM4/23/18
to wiremock-user
configureFor(...) gets called by the rule already, so you shouldn't need to do this.

But if you want to grab the port number for another reason you should be able to it inside a @Before method. Are you using JUnit 4.12?

Can I suggest posting the full test class?

Aziz Ben Miled

unread,
Apr 23, 2018, 10:56:14 AM4/23/18
to wiremock-user
yes, i'm using junit 4.12

public class NebefRefOeItemReaderTest {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
    String wireMockPort = String.valueOf(wireMockRule.port());
   
    RestTemplate restTemplate = new RestTemplate();
    String refOeAPI = "/api/v1/referentiel/ref-oe";
    String urlAPI = "http://localhost:" + wireMockPort + refOeAPI;
   
    NebefRefOeItemReader reader = new NebefRefOeItemReader(urlAPI, restTemplate);
    List<NebefRefOeProteine> result = reader.fetchOeDataFromAPI(urlAPI, restTemplate);

   
    @Before
    public void init() {
        WireMock.configureFor(wireMockRule.port());
    }
   

    @Test
    public void shouldReturnEmptyListWhenNoDataFromProteine() {
        stubFor(get(urlEqualTo(refOeAPI))
                .willReturn(aResponse().withStatus(200).withBodyFile("src/test/resources/oes.json")));

        assertNotNull(result.size());
    }

    @Test
    public void shouldReturnRightMappedDataFromProteine() {
        assertEquals(result.get(0).getNom(), "RES REACTIVE FLEXIBILITE SERVICES");
        assertEquals(result.get(0).getCodeEic(), "17X100A100F0085M");
        assertEquals(result.get(0).getCodeIsu(), "20971");
        assertEquals(result.get(0).getReEffaceur(), null);

        LocalDate ld = LocalDate.parse("2017-01-01");
        assertEquals(LocalDate.parse(Utils.formatSimpleDate(result.get(0).getDateDebutValidite())), ld);
        ld = LocalDate.parse("2099-12-31");
        assertEquals(LocalDate.parse(Utils.formatSimpleDate(result.get(0).getDateFinValidite())), ld);
        ZonedDateTime zdt = ZonedDateTime.parse("2017-04-24T22:00:00.000Z");
        assertEquals(ZonedDateTime.ofInstant(result.get(0).getDateDerniereModif().toInstant(), ZoneId.of("Z")), zdt);
    }

    @Test
    public void shouldReturnOnlyMostRecentDataFromProteine() {
        ZonedDateTime lastModif = ZonedDateTime.parse("2017-04-24T22:00:00.000Z");
        Iterator<NebefRefOeProteine> it = result.iterator();
        while (it.hasNext()) {
            assertTrue(ZonedDateTime.ofInstant(it.next().getDateDerniereModif().toInstant(), ZoneId.of("Z")) != null);
        }
        ZonedDateTime first = ZonedDateTime.ofInstant(result.get(0).getDateDerniereModif().toInstant(), ZoneId.of("Z"));
        long minutes = ChronoUnit.MINUTES.between(lastModif, first);
        System.out.println(minutes + " entre " + first.toString() + " et " + lastModif.toString());

    }

}



Le lundi 23 avril 2018 16:33:51 UTC+2, Aziz Ben Miled a écrit :

Tom Akehurst

unread,
Apr 23, 2018, 10:58:36 AM4/23/18
to wiremock-user
1) Reduce the field wireMockPort to a declaration i.e. String wireMockPort;
2) Move this line inside the @Before method:
wireMockPort = String.valueOf(wireMockRule.port());
3) Delete the WireMock.configureFor() call 

Tom Akehurst

unread,
Apr 23, 2018, 10:59:12 AM4/23/18
to wiremock-user
You'll also need to initialise any fields that depend on wireMockPort inside the @Before method, obviously.

Aziz Ben Miled

unread,
Apr 23, 2018, 11:07:04 AM4/23/18
to wiremock-user
still not work with :

public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

   
   
    RestTemplate restTemplate = new RestTemplate();
    String refOeAPI = "/api/v1/referentiel/ref-oe";
    String wireMockPort;
   
    @Before
    public void init() {
        wireMockPort = String.valueOf(wireMockRule.port());

       
    }
   
    String urlAPI = "http://localhost:" + wireMockPort + refOeAPI;
    NebefRefOeItemReader reader = new NebefRefOeItemReader(urlAPI, restTemplate);
    List<NebefRefOeProteine> result = reader.fetchOeDataFromAPI(urlAPI, restTemplate);

the new exception say :

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost/null/api/v1/referentiel/ref-oe": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect


Le lundi 23 avril 2018 16:33:51 UTC+2, Aziz Ben Miled a écrit :

Tom Akehurst

unread,
Apr 23, 2018, 11:10:08 AM4/23/18
to wiremock-user
There are two problems with the way you're building your URLs: 1) the /null bit presumably should be a value other than null, 2) you're not including the port number, so it'll be defaulting to port 80
Reply all
Reply to author
Forward
0 new messages