@Override
	public Statement apply(final Statement base, final Description description) {
		return new Statement() {

			@Override
			public void evaluate() throws Throwable {
				PactVerifications pactVerifications = description.getAnnotation(PactVerifications.class);
				if (pactVerifications != null) {
					evaluatePactVerifications(pactVerifications, base, description);
					return;
				}

				PactVerification pactDef = description.getAnnotation(PactVerification.class);
				// no pactVerification? execute the test normally
				if (pactDef == null) {
					base.evaluate();
					return;
				}

				Message providedMessage = null;
				Map<String, Message> pacts;
				if (StringUtils.isNoneEmpty(pactDef.fragment())) {
          Optional<Method> possiblePactMethod = findPactMethod(pactDef);
          if (!possiblePactMethod.isPresent()) {
            base.evaluate();
            return;
          }

          pacts = new HashMap<>();
          Method method = possiblePactMethod.get();
          Pact pact = method.getAnnotation(Pact.class);
          MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
          messagePact = (MessagePact) method.invoke(testClassInstance, builder);
          for (Message message : messagePact.getMessages()) {
            pacts.put(message.getProviderStates().stream().map(ProviderState::getName).collect(Collectors.joining()),
							message);
          }
        } else {
          pacts = parsePacts();
        }

        if (pactDef.value().length == 2 && !pactDef.value()[1].trim().isEmpty()) {
          providedMessage = pacts.get(pactDef.value()[1].trim());
        } else if (!pacts.isEmpty()) {
          providedMessage = pacts.values().iterator().next();
        }

				if (providedMessage == null) {
					base.evaluate();
					return;
				}

				setMessage(providedMessage, description);
				try {
					base.evaluate();
					PactFolder pactFolder = testClassInstance.getClass().getAnnotation(PactFolder.class);
					if (pactFolder != null) {
						messagePact.write(pactFolder.value(), PactSpecVersion.V3);
					} else {
						messagePact.write(PactConsumerConfig.INSTANCE.getPactDirectory(), PactSpecVersion.V3);
					}
				} catch (Throwable t) {
					throw t;
				}
			}
		};
	}
@ Override public Statement apply ( final Statement base , final Description description ) { return new Statement ( ) { @ Override public void evaluate ( ) throws Throwable { PactVerifications pactVerifications = description . getAnnotation ( PactVerifications . class ) ; if ( pactVerifications != null ) { evaluatePactVerifications ( pactVerifications , base , description ) ; return ; } PactVerification pactDef = description . getAnnotation ( PactVerification . class ) ; if ( pactDef == null ) { base . evaluate ( ) ; return ; } Message providedMessage = null ; Map < String , Message > pacts ; if ( StringUtils . isNoneEmpty ( pactDef . fragment ( ) ) ) { Optional < Method > possiblePactMethod = findPactMethod ( pactDef ) ; if ( ! possiblePactMethod . isPresent ( ) ) { base . evaluate ( ) ; return ; } pacts = new HashMap < > ( ) ; Method method = possiblePactMethod . get ( ) ; Pact pact = method . getAnnotation ( Pact . class ) ; MessagePactBuilder builder = MessagePactBuilder . consumer ( pact . consumer ( ) ) . hasPactWith ( provider ) ; messagePact = ( MessagePact ) method . invoke ( testClassInstance , builder ) ; for ( Message message : messagePact . getMessages ( ) ) { pacts . put ( message . getProviderStates ( ) . stream ( ) . map ( ProviderState :: getName ) . collect ( Collectors . joining ( ) ) , message ) ; } } else { pacts = parsePacts ( ) ; } if ( pactDef . value ( ) . length == 2 && ! pactDef . value ( ) [ 1 ] . trim ( ) . isEmpty ( ) ) { providedMessage = pacts . get ( pactDef . value ( ) [ 1 ] . trim ( ) ) ; } else if ( ! pacts . isEmpty ( ) ) { providedMessage = pacts . values ( ) . iterator ( ) . next ( ) ; } if ( providedMessage == null ) { base . evaluate ( ) ; return ; } setMessage ( providedMessage , description ) ; try { base . evaluate ( ) ; PactFolder pactFolder = testClassInstance . getClass ( ) . getAnnotation ( PactFolder . class ) ; if ( pactFolder != null ) { messagePact . write ( pactFolder . value ( ) , PactSpecVersion . V3 ) ; } else { messagePact . write ( PactConsumerConfig . INSTANCE . getPactDirectory ( ) , PactSpecVersion . V3 ) ; } } catch ( Throwable t ) { throw t ; } } } ; }
/* (non-Javadoc)
@see org.junit.rules.ExternalResource#apply(org.junit.runners.model.Statement, org.junit.runner.Description)