diff --git a/pom.xml b/pom.xml index c4f6ce5182f4b96368966dfb9442c8b54cd0f79d..900356a507d02742d3b968f11eb4ec5522890b01 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ mms integrate-mybatis integrate-mybatisplus + testing org.springframework.boot diff --git a/testing/pom.xml b/testing/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..623736e4260d4ebca1ed38bf54380ea790f2e9b1 --- /dev/null +++ b/testing/pom.xml @@ -0,0 +1,30 @@ + + + + springboot-demo + com.pannk + 1.0 + + 4.0.0 + + testing + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.jupiter + junit-jupiter-api + + + + + \ No newline at end of file diff --git a/testing/src/main/java/com/pannk/demo/App.java b/testing/src/main/java/com/pannk/demo/App.java new file mode 100644 index 0000000000000000000000000000000000000000..358d026133abae2855c1e55944ec8d9ec002887d --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/App.java @@ -0,0 +1,14 @@ +package com.pannk.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by wolf on 20-11-10. + */ +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class,args); + } +} diff --git a/testing/src/main/java/com/pannk/demo/controller/HelloController.java b/testing/src/main/java/com/pannk/demo/controller/HelloController.java new file mode 100644 index 0000000000000000000000000000000000000000..b0cb17427e7de71794c320427f70d2061023e92f --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/controller/HelloController.java @@ -0,0 +1,21 @@ +package com.pannk.demo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by wolf on 20-11-10. + */ +@RestController +public class HelloController { + + @RequestMapping("/") + public String index() { + return "index"; + } + + @RequestMapping("/hello") + public String hello() { + return "hello"; + } +} diff --git a/testing/src/main/java/com/pannk/demo/service/CallService.java b/testing/src/main/java/com/pannk/demo/service/CallService.java new file mode 100644 index 0000000000000000000000000000000000000000..082de27ed557d00a117b89f88eae5a43976291eb --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/service/CallService.java @@ -0,0 +1,12 @@ +package com.pannk.demo.service; + +/** + * Created by wolf on 20-11-10. + */ +public interface CallService { + + /** + * 调用方法 + */ + void call(); +} diff --git a/testing/src/main/java/com/pannk/demo/service/HelloService.java b/testing/src/main/java/com/pannk/demo/service/HelloService.java new file mode 100644 index 0000000000000000000000000000000000000000..17a10fe343495a1d6ffc593956e86772792a2e18 --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/service/HelloService.java @@ -0,0 +1,12 @@ +package com.pannk.demo.service; + +/** + * Created by wolf on 20-11-10. + */ +public interface HelloService { + + /** hello + * @return + */ + String hello(); +} diff --git a/testing/src/main/java/com/pannk/demo/service/impl/CallServiceImpl.java b/testing/src/main/java/com/pannk/demo/service/impl/CallServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..416ed5d13b0dfa9e7002ed5682648bfb2bc46167 --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/service/impl/CallServiceImpl.java @@ -0,0 +1,23 @@ +package com.pannk.demo.service.impl; + +import com.pannk.demo.service.CallService; +import com.pannk.demo.service.HelloService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * Created by wolf on 20-11-10. + */ +@Service +public class CallServiceImpl implements CallService { + + @Autowired + private HelloService helloService; + + @Override + public void call() { + System.out.println("调用HelloService方法"); + String result = helloService.hello(); + System.out.println("返回结果为:"+result); + } +} diff --git a/testing/src/main/java/com/pannk/demo/service/impl/HelloServiceImpl.java b/testing/src/main/java/com/pannk/demo/service/impl/HelloServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..bf73f8ae35603f67898bb264367d892bd3d57e83 --- /dev/null +++ b/testing/src/main/java/com/pannk/demo/service/impl/HelloServiceImpl.java @@ -0,0 +1,16 @@ +package com.pannk.demo.service.impl; + +import com.pannk.demo.service.HelloService; +import org.springframework.stereotype.Service; + +/** + * Created by wolf on 20-11-10. + */ +@Service +public class HelloServiceImpl implements HelloService { + + @Override + public String hello(){ + return "hello"; + } +} diff --git a/testing/src/main/resources/application.yml b/testing/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/testing/src/test/java/com/pannk/demo/controller/HelloControllerMockTest.java b/testing/src/test/java/com/pannk/demo/controller/HelloControllerMockTest.java new file mode 100644 index 0000000000000000000000000000000000000000..52a6a1e794ed558eed853b81526baa31578e69a6 --- /dev/null +++ b/testing/src/test/java/com/pannk/demo/controller/HelloControllerMockTest.java @@ -0,0 +1,40 @@ +package com.pannk.demo.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.net.URI; + +/** + * Created by wolf on 20-11-10. + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)//MOCK为默认值,也可不设置 +@AutoConfigureMockMvc +public class HelloControllerMockTest { + + @Autowired + private MockMvc mvc; + + @Test + public void testIndex() throws Exception{ + ResultActions ra = mvc.perform(MockMvcRequestBuilders.get(new URI("/"))); + MvcResult result = ra.andReturn(); + System.out.println(result.getResponse().getContentAsString()); + } + + @Test + public void testHello() throws Exception{ + ResultActions ra = mvc.perform(MockMvcRequestBuilders.get(new URI("/hello"))); + MvcResult result = ra.andReturn(); + System.out.println(result.getResponse().getContentAsString()); + } +} diff --git a/testing/src/test/java/com/pannk/demo/controller/HelloControllerTest.java b/testing/src/test/java/com/pannk/demo/controller/HelloControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5b5b432528783b46c9d2e84d79618143fd29ee7e --- /dev/null +++ b/testing/src/test/java/com/pannk/demo/controller/HelloControllerTest.java @@ -0,0 +1,36 @@ +package com.pannk.demo.controller; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Created by wolf on 20-11-10. + * WebEnvironment.RANDOM_PORT会启动一个真实的Web容器,RANDOM_PORT表示随机端口 + * 如果想使用固定端口,可配置为 + * WebEnvironment.DEFINED_PORT,该属性会读取项目配置文件(如application.yml)中的端口(server.port) + * 如果没有配置,默认使用8080端口 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class HelloControllerTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void testIndex(){ + String result = restTemplate.getForObject("/",String.class); + Assert.assertEquals("index", result); + } + + @Test + public void testHello(){ + String result = restTemplate.getForObject("/",String.class); + Assert.assertEquals("Hello world", result);//这里故意写错 + } +} \ No newline at end of file diff --git a/testing/src/test/java/com/pannk/demo/service/impl/CallServiceImplTest.java b/testing/src/test/java/com/pannk/demo/service/impl/CallServiceImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..172ded145176b194de39d9034176d806510937f0 --- /dev/null +++ b/testing/src/test/java/com/pannk/demo/service/impl/CallServiceImplTest.java @@ -0,0 +1,30 @@ +package com.pannk.demo.service.impl; + +import com.pannk.demo.service.CallService; +import com.pannk.demo.service.HelloService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Created by wolf on 20-11-10. + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class CallServiceImplTest { + + @MockBean + private HelloService helloService; + @Autowired + private CallService mainService; + + @Test + public void testCall(){ + BDDMockito.given(this.helloService.hello()).willReturn("hello world"); + mainService.call(); + } +} diff --git a/testing/src/test/java/com/pannk/demo/service/impl/HelloServiceImplTest.java b/testing/src/test/java/com/pannk/demo/service/impl/HelloServiceImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bc4e0c08954afd2d2d075be847db189981bc87e5 --- /dev/null +++ b/testing/src/test/java/com/pannk/demo/service/impl/HelloServiceImplTest.java @@ -0,0 +1,25 @@ +package com.pannk.demo.service.impl; + +import com.pannk.demo.service.HelloService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Created by wolf on 20-11-10. + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +public class HelloServiceImplTest { + @Autowired + private HelloService helloService; + + @Test + public void testHello(){ + String result = helloService.hello(); + System.out.println(result); + } + +}