Skip to content

Commit

Permalink
单元测试部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
rbmonster committed Mar 17, 2021
1 parent eae3ee6 commit f82e05c
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class TestInterceptorController {
public void test() {
log.info("this is interceptor");
}

@GetMapping("/one")
public String fixReturn() {
return "123";
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/four/transaction/controller/UnitController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.four.transaction.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: UnitController
* @Author: sanwu
* @Date: 2021/3/18 0:25
*/
@RestController
@RequestMapping("/unit")
public class UnitController {

@GetMapping("/one")
public String fixReturn() {
return "For one";
}

@PostMapping("/upload")
public List<String> upload(List<String> list) {
System.out.println(Arrays.toString(list.toArray()));
return Arrays.asList("success", "fail");
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/learning/basic/UNIT_TEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 单元测试


BDDMockito

Mockito
83 changes: 83 additions & 0 deletions src/test/java/SpringTestDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import com.four.filterinterceptor.interceptor.TestInterceptorController;
import com.four.transaction.TransactionApplication;
import org.junit.Before;
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.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: SpringTestDemo
* @Author: sanwu
* @Date: 2021/3/16 22:28
*/

@AutoConfigureMockMvc
@ActiveProfiles(profiles = "test")
@SpringBootTest(classes = TransactionApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class SpringTestDemo {

@LocalServerPort
private int port;

@MockBean
private JdbcTemplate jdbcTemplate;

@MockBean
private PlatformTransactionManager platformTransactionManager;

@MockBean
private TransactionTemplate transactionTemplate;

@Before
public void setUp() {

}

@Autowired
private TestRestTemplate restTemplate;

@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
String forObject = this.restTemplate.getForObject("http://localhost:" + port + "/learning/unit/one", String.class);
System.out.println(forObject);
}

@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/unit/one"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("one")));
}
}
44 changes: 44 additions & 0 deletions src/test/java/WebLayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import com.four.transaction.controller.UnitController;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* <pre>
* @Description:
* TODO 有问题
* </pre>
*
* @version v1.0
* @ClassName: WebLayerTest
* @Author: sanwu
* @Date: 2021/3/18 0:38
*/
//@RunWith(SpringRunner.class)
@WebMvcTest(controllers = UnitController.class)
@AutoConfigureMockMvc(addFilters = false)
public class WebLayerTest {

@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/unit/one"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("one")));
}
}
37 changes: 37 additions & 0 deletions src/test/resource/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
spring:
autoconfigure:
exclude:
- com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
- org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
- org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration
- org.redisson.spring.cache.RedissonCacheStatisticsAutoConfiguration
- org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration
- org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration
- org.springframework.cloud.autoconfigure.RefreshAutoConfiguration
- org.springframework.cloud.openfeign.FeignAutoConfiguration
- org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration
- org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration
- org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration
- org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration
- org.springframework.cloud.client.CommonsClientAutoConfiguration
- org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration
- org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration
- org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration
- org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration
- org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration
- org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration
- org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration
- org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration
- org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration
- org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration
- org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerClientAutoConfiguration
- org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancerAutoConfiguration
- org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration
- org.springframework.cloud.commons.httpclient.HttpClientConfiguration
- org.springframework.cloud.commons.util.UtilAutoConfiguration
- org.springframework.cloud.configuration.CompatibilityVerifierAutoConfiguration
- org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration

0 comments on commit f82e05c

Please sign in to comment.