0

I have created a Spring Cloud Contract stub in a Spring Boot project (spring-server). The client that wants to call this stub is not a Spring project and cannot be one. If I run the following in the client:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = {"uk.co.hadoopathome:spring-server:+:stubs:8093"},
        stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class ContractTest {
    @Test
    public void testContractEndpoint() {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet("http://localhost:8093/ac01");
            CloseableHttpResponse response = httpclient.execute(httpGet);
            String entity = EntityUtils.toString(response.getEntity());
            assertEquals("ac01returned", entity);
            response.close();
        } catch (IOException ignored) {
        }
    }
}

then I get an error

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

Obviously I don't have a @SpringBootConfiguration, as this isn't a Spring Boot project.

What's the workaround here?

Ben Watson
  • 5,357
  • 4
  • 42
  • 65

2 Answers2

1

Just use the Junit rule and you won't have to setup a context

public class JUnitTest {

    @Rule public StubRunnerRule rule = new StubRunnerRule()
            .downloadStub("com.example","beer-api-producer")
            .withPort(6543)
            .workOffline(true);

    @Test
    public void should_work() {
        String response = new RestTemplate().getForObject("http://localhost:6543/status", String.class);

        BDDAssertions.then(response).isEqualTo("ok");
    }
Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Could you elaborate a bit please? I found https://cloud.spring.io/spring-cloud-contract/1.0.x/#_alternative_using_junit_rules, but you have to create your own stubs in the client code, which isn't what I'm looking for here. – Ben Watson Apr 27 '18 at 14:36
  • https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_stub_runner_junit_rule and samples and https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/consumer/src/test/java/com/example/BeerControllerWithJUnitTest.java we really invest time in the docs but you have to check the latest release train – Marcin Grzejszczak Apr 27 '18 at 14:44
  • Perfect, thank you. The docs are very good, I was just unlucky with my Ctrl+F's :) – Ben Watson Apr 27 '18 at 14:48
  • I'm implementing it right now and will mark when it's green. – Ben Watson Apr 27 '18 at 14:50
  • https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/consumer/src/test/java/com/example/BeerControllerWithJUnitTest.java still requires the `RunWith(SpringRunner)` and `SpringBootTest` annotations, and so I get my original error unless I make the change I mention in my answer. Am I missing something? – Ben Watson Apr 27 '18 at 15:03
  • It doesn't require springboottest. If i have it in the sample then it's wrong. You don't need a spring runner either. You just need a normal junit test – Marcin Grzejszczak Apr 28 '18 at 01:10
  • It does work if all class-level annotations from https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/consumer/src/test/java/com/example/BeerControllerWithJUnitTest.java are removed. I also dont use `MockMvc`. – Ben Watson Apr 30 '18 at 08:43
  • Great! Do i guess we can mark it as resolved. I will add an issue to remove the unnecessary annotations – Marcin Grzejszczak Apr 30 '18 at 11:33
  • It would be great to have a working example as the answer; at the moment it's just a one-liner which is probably a bit too short to help other people in the future. I tried to add my working code as an edit but it was rejected. If you copy and paste the documentation code when the issue is resolved, I'll accept this answer. – Ben Watson Apr 30 '18 at 12:13
  • I've editted the answer. I can't modify the samples cause they combine the JUnit rule to download the stubs and the usage of mock mvc to test the controller. – Marcin Grzejszczak Apr 30 '18 at 13:00
0

I modified the @SpringBootTest line:

@SpringBootTest(classes = ContractTest.class)

and then got some Logback errors which I resolved by finding this answer and adding to build.gradle:

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
}
Ben Watson
  • 5,357
  • 4
  • 42
  • 65