File tree 7 files changed +205
-0
lines changed
test/java/net/coding/boot
7 files changed +205
-0
lines changed Original file line number Diff line number Diff line change
1
+ /target /
2
+ /.mvn /
3
+ ! .mvn /wrapper /maven-wrapper.jar
4
+
5
+ # ## STS ###
6
+ .apt_generated
7
+ .classpath
8
+ .factorypath
9
+ .project
10
+ .settings
11
+ .springBeans
12
+ .sts4-cache
13
+
14
+ # ## IntelliJ IDEA ###
15
+ .idea
16
+ * .iws
17
+ * .iml
18
+ * .ipr
19
+
20
+ # ## NetBeans ###
21
+ /nbproject /private /
22
+ /build /
23
+ /nbbuild /
24
+ /dist /
25
+ /nbdist /
26
+ /.nb-gradle /
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+ <modelVersion >4.0.0</modelVersion >
5
+
6
+ <groupId >net.coding</groupId >
7
+ <artifactId >springboot-hello</artifactId >
8
+ <version >0.0.1-SNAPSHOT</version >
9
+ <packaging >jar</packaging >
10
+
11
+ <name >springboot-hello</name >
12
+ <description >springboot-hello</description >
13
+
14
+ <parent >
15
+ <groupId >org.springframework.boot</groupId >
16
+ <artifactId >spring-boot-starter-parent</artifactId >
17
+ <version >2.1.1.RELEASE</version >
18
+ <relativePath /> <!-- lookup parent from repository -->
19
+ </parent >
20
+
21
+ <dependencies >
22
+ <dependency >
23
+ <groupId >org.springframework.boot</groupId >
24
+ <artifactId >spring-boot-starter-web</artifactId >
25
+ </dependency >
26
+
27
+ <dependency >
28
+ <groupId >org.springframework.boot</groupId >
29
+ <artifactId >spring-boot-starter-test</artifactId >
30
+ <scope >test</scope >
31
+ </dependency >
32
+
33
+ </dependencies >
34
+
35
+ <build >
36
+ <plugins >
37
+ <plugin >
38
+ <groupId >org.springframework.boot</groupId >
39
+ <artifactId >spring-boot-maven-plugin</artifactId >
40
+ </plugin >
41
+ </plugins >
42
+ </build >
43
+
44
+
45
+ </project >
Original file line number Diff line number Diff line change
1
+ package net .coding .boot ;
2
+
3
+ import org .springframework .boot .CommandLineRunner ;
4
+ import org .springframework .boot .SpringApplication ;
5
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6
+ import org .springframework .context .ApplicationContext ;
7
+ import org .springframework .context .annotation .Bean ;
8
+
9
+ import java .util .Arrays ;
10
+
11
+ @ SpringBootApplication
12
+ public class HelloApplication {
13
+
14
+ public static void main (String [] args ) {
15
+ SpringApplication .run (HelloApplication .class , args );
16
+ }
17
+
18
+ @ Bean
19
+ public CommandLineRunner commandLineRunner (ApplicationContext ctx ) {
20
+ return args -> {
21
+ // 开始检查spring boot 提供的 beans
22
+ System .out .println ("Let's inspect the beans provided by Spring Boot:" );
23
+ String [] beanNames = ctx .getBeanDefinitionNames ();
24
+ Arrays .sort (beanNames );
25
+ for (String beanName : beanNames ) {
26
+ System .out .println (beanName );
27
+ }
28
+ };
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ package net .coding .boot .controller ;
2
+
3
+ import org .springframework .context .annotation .Configuration ;
4
+ import org .springframework .web .bind .annotation .RequestMapping ;
5
+ import org .springframework .web .bind .annotation .ResponseBody ;
6
+ import org .springframework .web .bind .annotation .RestController ;
7
+
8
+ /**
9
+ * <p>
10
+ *
11
+ * @Author niujinpeng
12
+ * @Date 2018/12/4 14:41
13
+ */
14
+ @ RestController
15
+ public class HelloController {
16
+
17
+ @ RequestMapping ("/" )
18
+ public String index () {
19
+ return "Greetings from Spring Boot!" ;
20
+ }
21
+
22
+ }
Original file line number Diff line number Diff line change
1
+ server.port =8080
Original file line number Diff line number Diff line change
1
+ package net .coding .boot ;
2
+
3
+
4
+ import org .junit .Before ;
5
+ import org .junit .Test ;
6
+ import org .junit .runner .RunWith ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .boot .test .context .SpringBootTest ;
9
+ import org .springframework .boot .test .web .client .TestRestTemplate ;
10
+ import org .springframework .boot .web .server .LocalServerPort ;
11
+ import org .springframework .http .ResponseEntity ;
12
+ import org .springframework .test .context .junit4 .SpringRunner ;
13
+
14
+ import java .net .URL ;
15
+
16
+ /**
17
+ * <p>
18
+ * 嵌入式服务器由随机端口启动webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
19
+ * 并且在运行时发现实际端口@LocalServerPort
20
+ *
21
+ * @Author niujinpeng
22
+ * @Date 2018/12/4 15:02
23
+ */
24
+ @ RunWith (SpringRunner .class )
25
+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
26
+ public class HelloApplicationTestBySpringBoot {
27
+
28
+ @ LocalServerPort
29
+ private int port ;
30
+
31
+ private URL base ;
32
+
33
+ @ Autowired
34
+ private TestRestTemplate template ;
35
+
36
+ @ Before
37
+ public void setup () throws Exception {
38
+ this .base = new URL ("http://localhost:" + port + "/" );
39
+ }
40
+
41
+ @ Test
42
+ public void getHello () throws Exception {
43
+ ResponseEntity <String > response = template .getForEntity (base .toString (), String .class );
44
+ assert (response .getBody ().equals ("Greetings from Spring Boot!" ));
45
+ }
46
+
47
+ }
Original file line number Diff line number Diff line change
1
+ package net .coding .boot ;
2
+
3
+ import org .junit .Test ;
4
+ import org .junit .runner .RunWith ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
7
+ import org .springframework .boot .test .context .SpringBootTest ;
8
+ import org .springframework .http .MediaType ;
9
+ import org .springframework .test .context .junit4 .SpringRunner ;
10
+ import org .springframework .test .web .servlet .MockMvc ;
11
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
12
+
13
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
14
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
15
+
16
+ /**
17
+ * 单元测试
18
+ */
19
+ @ RunWith (SpringRunner .class )
20
+ @ SpringBootTest
21
+ @ AutoConfigureMockMvc
22
+ public class HelloApplicationTests {
23
+
24
+ @ Autowired
25
+ private MockMvc mvc ;
26
+
27
+ @ Test
28
+ public void testGetHello () throws Exception {
29
+ mvc .perform (MockMvcRequestBuilders .get ("/" ).accept (MediaType .APPLICATION_JSON ))
30
+ .andExpect (status ().isOk ())
31
+ .andExpect (content ().string ("Greetings from Spring Boot!" ));
32
+ }
33
+
34
+ }
You can’t perform that action at this time.
0 commit comments