提交 d933ea83 编写于 作者: Q qinxiaodong@pannk.com

更新

上级 213cc407
......@@ -14,6 +14,7 @@
<module>mms</module>
<module>integrate-mybatis</module>
<module>integrate-mybatisplus</module>
<module>testing</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.pannk</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testing</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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);
}
}
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";
}
}
package com.pannk.demo.service;
/**
* Created by wolf on 20-11-10.
*/
public interface CallService {
/**
* 调用方法
*/
void call();
}
package com.pannk.demo.service;
/**
* Created by wolf on 20-11-10.
*/
public interface HelloService {
/** hello
* @return
*/
String hello();
}
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);
}
}
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";
}
}
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());
}
}
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
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();
}
}
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册