-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathBeerControllerEmptyGitTest.java
More file actions
88 lines (69 loc) · 3.18 KB
/
BeerControllerEmptyGitTest.java
File metadata and controls
88 lines (69 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.stubrunner.junit.StubRunnerExtension;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.StringUtils;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Marcin Grzejszczak
*/
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
// example of usage with fixed port
@DirtiesContext
@org.junit.jupiter.api.Disabled("Pre-existing: git contract resolution fails with stub mismatch")
public class BeerControllerEmptyGitTest extends AbstractTest {
@Autowired MockMvc mockMvc;
@Autowired BeerController beerController;
@RegisterExtension
static StubRunnerExtension rule = new StubRunnerExtension()
.downloadStub("com.example","beer-api-producer-empty-git", "0.0.1-SNAPSHOT")
.repoRoot("git://" + System.getenv("ROOT") + "/target/contract_empty_git/")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
@BeforeAll
public static void beforeClass() {
Assumptions.assumeTrue(atLeast210(), "Spring Cloud Contract must be in version at least 2.1.0");
Assumptions.assumeTrue(StringUtils.isEmpty(System.getenv("OLD_PRODUCER_TRAIN")), "Env var OLD_PRODUCER_TRAIN must not be set");
}
@BeforeEach
public void setupPort() {
this.beerController.port = rule.findStubUrl("beer-api-producer-empty-git").getPort();
}
private static boolean atLeast210() {
try {
Class.forName("org.springframework.cloud.contract.verifier.util.ContractVerifierUtil");
} catch (Exception ex) {
return false;
}
return true;
}
@Test public void should_give_me_a_beer_when_im_old_enough() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/beer")
.contentType(MediaType.APPLICATION_JSON)
.content(this.json.write(new Person("marcin", 22)).getJson()))
.andExpect(status().isOk())
.andExpect(content().string("THERE YOU GO"));
}
@Test public void should_reject_a_beer_when_im_too_young() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/beer")
.contentType(MediaType.APPLICATION_JSON)
.content(this.json.write(new Person("marcin", 17)).getJson()))
.andExpect(status().isOk())
.andExpect(content().string("GET LOST"));
}
}