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.09k stars 480 forks source link

AmpqTestTarget not finding @PactVerifyProvider, JUnit5, Java 11 #1053

Open mkluzacek opened 4 years ago

mkluzacek commented 4 years ago

Hi, i am having the same issue as mentioned here https://github.com/DiUS/pact-jvm/issues/763.

Using:

<dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit5_2.12</artifactId>
            <version>3.6.14</version>
 </dependency>

And :

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
    <useSystemClassLoader>false</useSystemClassLoader>
</configuration>

My test looks like this:

@Provider("user-service")
@PactBroker
public class UserPactTest {

    @TestTemplate
    @ExtendWith({PactVerificationInvocationContextProvider.class})
    public void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @BeforeEach
    public void setUp(PactVerificationContext context) {
        context.setTarget(new AmpqTestTarget(Collections.singletonList("my.package.pact")));
    }

    @PactVerifyProvider("user created message")
    public String verifyUserCreatedMessage() throws JsonProcessingException {
        // some code that just returns string from object with object mapper
    }
}

I've tried multiple version of surefire plugin, tried using empty constructor for AmpqTestTarget but everything ends with: user created message No annotated methods were found for interaction 'user created message'. You need to provide a method annotated with @PactVerifyProvider("user created message") that returns the message contents.

The test doesnt work from both IDE(Intellij) and maven. I just cant get it to work so any help would be appreciated.

uglyog commented 4 years ago

Check that

context.setTarget(new AmpqTestTarget(Collections.singletonList("my.package.pact")));

has the correct package for you message handler.

I've added your test to https://github.com/uglyog/pact-maven-amqp-test (see https://github.com/uglyog/pact-maven-amqp-test/blob/master/provider/src/test/java/com/github/uglyog/mavenamqp/UserPactTest.java).

That project works, so you should be able to compare it to yours.

mkluzacek commented 4 years ago

Thank you very much for your help. I was finally able to fix it. Problem was the dependency. I was using:

<dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit5_2.12</artifactId>
            <version>3.6.14</version>
</dependency>

Instead of :

<dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-provider-junit5</artifactId>
            <version>4.0.7</version>
            <scope>test</scope>
</dependency>

No idea where i found the first one. Thanks again.

uglyog commented 4 years ago

That was the old (pre-4.x) dependency.

Norman-else commented 4 years ago

SpringBootHttpTarget also cannot find annotated methods.

The following is my code of Pact in Provider:

@RunWith(SpringRestPactRunner.class)
@Provider("user")
@PactBroker
@ActiveProfiles("local")
@SpringBootTest(
    classes = EquipmentServiceLauncher.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public class EquipmentsProviderPact {

    @Autowired
    private UserRepository userRepository;

    @TestTarget
    public final SpringBootHttpTarget target = new SpringBootHttpTarget();

    @PactVerifyProvider(value = "GET User BY ID")
    public Map<String, Object> setupUser() {
        ...
    }
}
uglyog commented 4 years ago

@Norman-else looks like you are using the wrong annotation. Is that method a state change handler? If so, it should be annotated with the @State annotation.

jespernordSPL commented 3 years ago

I upgraded a service to Java 11 + Junit 5, got the same problem. In my case it was because of a trailing star in the packages list:

@TestTarget public final Target target = new AmqpTarget(Collections.singletonList("com.example.my.package.*"));

which had to be replaced with (no star at end of package):

@BeforeEach
void before(PactVerificationContext context) {
    context.setTarget(new MessageTestTarget(List.of("com.example.my.package")));
}