forked from rbmonster/learning-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/four/transaction/controller/UnitController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 单元测试 | ||
|
||
|
||
BDDMockito | ||
|
||
Mockito |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |