Search code examples
pact

Do we need to do something with the @TestTarget variable in Pact?


I'm learning Pact currently and have a query regarding @TestTarget annotation. To point a class to a target we must initialize a variable with either of the 3 types of target (http, https and message) and add this @TestTarget annotation before the variable definition.

  1. But do we need to do something with the variable after that or is that the end of its usage?

  2. Once we've defined a TestTarget this way, can we say that all Pact tests that are defined inside this class will automatically pick this target?

    import au.com.dius.pact.provider.junit.PactRunner; 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.loader.PactUrl; import au.com.dius.pact.provider.junit.target.HttpTarget; import au.com.dius.pact.provider.junit.target.Target; import au.com.dius.pact.provider.junit.target.TestTarget; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.runner.RunWith;

    import java.net.URL; import java.util.Map;

    @RunWith(PactRunner.class) // Say JUnit to run tests with custom Runner @Provider("getCountryService") // Set up name of tested provider @PactFolder("../pacts") // Point where to find pacts (See also section Pacts source in documentation)

    //@PactUrl(urls = {"http://services.groupkt.com/country/get/iso3code/IND"} )

    public class getCountryContractTest {

     @State("There is a country with alpha2_code as IN having name as India") // Method will be run before testing
                                                                                 // interactions that require "with-data"
                                                                                 // state
     public void hammerSmith() {
         System.out.println("There is a country with alpha2_code as IN having name as India");
     }
    
     @TestTarget // Annotation denotes Target that will be used for tests
     public final Target target = new HttpTarget("http", "services.groupkt.com",);
    

    }


Solution

    1. No, just declare TestTarget. Pact library will use it for its needs.
    2. Yes, it will do automatically