pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 478 forks source link

java.lang.NumberFormatException: null #996

Open LayMui-Toh opened 4 years ago

LayMui-Toh commented 4 years ago

I get this error when running pact provider test

java.lang.NumberFormatException: null

at java.base/java.lang.Integer.parseInt(Integer.java:620)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at au.com.dius.pact.provider.spring.SpringInteractionRunner.setupTargetForInteraction(SpringInteractionRunner.kt:103)
at au.com.dius.pact.provider.junit.InteractionRunner$interactionBlock$statement$1.evaluate(InteractionRunner.kt:203)
at au.com.dius.pact.provider.junit.RunStateChanges.evaluate(RunStateChanges.kt:20)
at au.com.dius.pact.provider.spring.SpringBeforeRunner.evaluate(SpringInteractionRunner.kt:32)
at au.com.dius.pact.provider.spring.SpringAfterRunner.evaluate(SpringInteractionRunner.kt:48)
at au.com.dius.pact.provider.junit.InteractionRunner.run(InteractionRunner.kt:149)
at au.com.dius.pact.provider.junit.PactRunner.runChild(PactRunner.kt:137)
at au.com.dius.pact.provider.junit.PactRunner.runChild(PactRunner.kt:51)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Here is my pact file content: { "consumer": { "name": "Consumer" }, "provider": { "name": "Provider" }, "interactions": [ { "description": "accessToken", "providerState": "Authenticate with valid clientId", "request": { "method": "POST", "path": "/api/external/authenticate", "headers": { "Accept": "/", "Content-Type": "application/json" }, "body": { "clientId": "admin", "clientSecret": "admin" }, "matchingRules": { "$.body": { "match": "type" } } }, "response": { "status": 200, "headers": { "Content-Type": "application/json;charset=UTF-8" }, "body": { "accessToken": "dfasdfjdsakfjsdfjsadjfsdl", "timeLeftRefreshThresholdMs": 1800000, "authenticationErrorMsg": "null" }, "matchingRules": { "$.body": { "match": "type" } } }, "metadata": null } ], "metadata": { "pactSpecification": { "version": "2.0.0" } } }

here is my provider code snippet: package com.taiger.iconverse.admin.web.pact;

import au.com.dius.pact.provider.junit.Provider; import au.com.dius.pact.provider.junit.State; import au.com.dius.pact.provider.junit.loader.PactFolder; import au.com.dius.pact.provider.junit.target.Target; import au.com.dius.pact.provider.junit.target.TestTarget; import au.com.dius.pact.provider.spring.SpringRestPactRunner; import au.com.dius.pact.provider.spring.target.SpringBootHttpTarget; import com.taiger.admin.AdminApp; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource;

@RunWith(SpringRestPactRunner.class) @Provider("Provider") @PactFolder("src/test/resources/pacts") @SpringBootTest(classes = AdminApp.class) public class AuthenticatePactTest { @TestTarget public final Target target = new SpringBootHttpTarget();

    @State("Authenticate with valid clientId")
    public void authenticateTest() {
            System.out.println("Verifying AuthenticateTest");
    }

}

mefellows commented 4 years ago

I think you need to specify the local port the server is running on, either explicitly in a constructor or via env vars: https://github.com/DiUS/pact-jvm/blob/5d8bfd2d1f2220ac4a1fde90b7bab54d36d59874/provider/pact-jvm-provider-spring/src/main/kotlin/au/com/dius/pact/provider/spring/SpringInteractionRunner.kt#L103

mefellows commented 4 years ago

This is a good article on testing a spring boot app if you haven't read it https://reflectoring.io/consumer-driven-contract-provider-pact-spring/

uglyog commented 4 years ago

Try adding webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT to the @SpringBootTest annotation