Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitignore
  • Loading branch information
rbmonster committed Apr 28, 2021
2 parents 165e8d3 + 9e08c17 commit c3e7717
Show file tree
Hide file tree
Showing 22 changed files with 673 additions and 61 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target/
/.idea/
/learning-note.iml
/test.txt
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# learning-note
> Motivation、Passion、Evolution
一个技术点,面试官常见的三连问:
1. xxx是什么?
2. xxx优缺点是什么?
3. xxx应用及使用场景是什么?

## Java相关
- [Java基础](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/JAVA_BASE.md)
- [Java集合类](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/COLLECTION.md)
Expand Down
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-bom</artifactId>
<type>pom</type>
<version>7.46.0.Final</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
Expand Down Expand Up @@ -150,7 +161,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1.1-jre</version>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -195,6 +206,22 @@
<artifactId>shims</artifactId>
<version>0.7.45</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>


Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/four/unittest/service/UnitInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.four.unittest.service;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;


public interface UnitInterface {

void send(String str);

default String register(String str) {
return "register fail return default";
}

BigDecimal calculate(double num);
}
10 changes: 7 additions & 3 deletions src/main/java/com/four/unittest/service/UnitTestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
Expand All @@ -33,4 +32,9 @@ public String testJdbcTemplate() {
(resultSet, i) -> resultSet.getString(2));
return query.get(0).toString();
}


public String testParameter(int n, String str, BigDecimal bigDecimal, List<Integer> list, Map<Double, String> map) {
return n + str + bigDecimal + list + map;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/learning/basic/JAVA_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ RuntimeException是一种Unchecked Exception,即表示编译器不会检查程
- 在不指定泛型的情况下,泛型变量的类型为该方法中的几种类型的同一父类的最小级,直到Object
- 在指定泛型的情况下,该方法的几种类型必须是该泛型的实例的类型或者其子类
```
Number f = Test.add(1, 1.2); //这两个参数一个是Integer,以风格是Float,所以取同一父类的最小级,为Number
Object o = Test.add(1, "asd"); //这两个参数一个是Integer,以风格是Float,所以取同一父类的最小级,为Object
Number f = Test.add(1, 1.2); //这两个参数一个是Integer,另一个是Float,所以取同一父类的最小级,为Number
Object o = Test.add(1, "asd"); //这两个参数一个是Integer,另一个是String,所以取同一父类的最小级,为Object
//这是一个简单的泛型方法
public static <T> T add(T x,T y){
Expand Down
210 changes: 207 additions & 3 deletions src/main/java/com/learning/basic/UNIT_TEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TDD开发应该达到100%的单元测试覆盖率?
参考资料:
- [深度解读 - TDD(测试驱动开发)](https://www.jianshu.com/p/62f16cd4fef3)

## 相关资料
### 相关资料
- [InfoQ测试微服务之单元测试](https://www.infoq.cn/article/2017/11/Unit-test-micro-services)
- [微服务测试之单元测试](https://zhuanlan.zhihu.com/p/54267816)

Expand Down Expand Up @@ -136,7 +136,7 @@ public class JunitDemo {


测试用例demo
```
```java

@AutoConfigureMockMvc
@ActiveProfiles(profiles = "test")
Expand Down Expand Up @@ -196,8 +196,212 @@ public class JunitSpringTestDemo {

## testNG

### 基本介绍
TestNG不是一个JUnit扩展。它的灵感来源于JUnit。它的目的是优于JUnit的,尤其是当测试集成的类
-[与UnitTest比较](http://www.testng.org.cn/1723.html)

使用套件,指定某几个test中的group 为一个套件。在IDEA中直接运行xml文件,可以执行套件的测试。
```
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="testng-demo">
<test name="testGroup1">
<parameter name="dbconfig" value="db.properties!~" />
<parameter name="poolsize" value="10" />
<groups>
<run>
<include name="group1"/>
</run>
</groups>
<classes>
<class name="TestNGDemo"/>
</classes>
</test>
<test name="testGroup2">
<groups>
<run>
<include name="group2"/>
</run>
</groups>
<classes>
<class name="TestNGDemo"/>
</classes>
</test>
</suite>
```

- 基本注解使用的demo
```java
@Slf4j
public class TestNGDemo {

@BeforeSuite(groups = "group1")
public void testBeforeSuite() {
log.info("testBeforeSuite()");
}

@AfterSuite(groups = "group1")
public void testAfterSuite() {
log.info("testAfterSuite()");
}

@BeforeClass
public void setup(){
log.info("this is beforeClass");
}

@AfterClass
public void shutdown(){
log.info("this is AfterClass");
}

@BeforeMethod
public void testBeforeMethod(){
log.info("this is beforeMethod");
}

@AfterMethod
public void testAfterMethod(){
log.info("this is afterMethod");
}

@BeforeGroups("group1")
public void setupGroup1() {
log.info("setupGroup1()");
}

@AfterGroups("group1")
public void cleanGroup1() {
log.info("cleanGroup1()");
}


@Test(groups = "group1")
public void test1() {
log.info("this is test1");
}

@Test(groups = "group1", expectedExceptions = ArithmeticException.class)
public void testException() {
log.info("this is test2");
int i = 1 / 0;
log.info("After division the value of i is :"+ i);
}

@Test(enabled = false)
public void testIgnoreTestcase() {
log.info("~~~~~~~~~~~~~~~~~ignore ");
}

@Test(groups = "group2", timeOut = 5000) // time in mulliseconds
public void testThisShouldPass() throws InterruptedException {
Thread.sleep(4000);
}

@Test(groups = "group1", dependsOnMethods = "test1")
public void testDependOn() {
log.info("this is dependOnMethod~");
}

@Test(groups = "group1")
@Parameters({ "dbconfig", "poolsize" })
public void createConnection(String dbconfig, int poolsize) {
log.info("this is parameter Test, parameter is: {}, {}", dbconfig, poolsize);
}

@Test(groups = "group2", dataProvider = "addDataProvider")
public void testDataProvider( String data) {
log.info("this is testDataProvider, test data: {}",data);
}

@DataProvider
public Iterator<Object> addDataProvider() {
List<Object> list = Lists.newArrayList();
list.add("123");
list.add(("123wesdf"));
return list.iterator();
}
}
```
### spring相关的使用
通过`extends AbstractTestNGSpringContextTests`提供一个测试使用的spring 上下文,类似于Junit4中@RunWith(SpringExtend)
> AbstractTestNGSpringContextTests is an abstract base test class that integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a TestNG environment. When you extend AbstractTestNGSpringContextTests, you can access a protected applicationContext instance variable that you can use to perform explicit bean lookups or to test the state of the context as a whole.
### 相关资料
- [易百中文文档](https://www.yiibai.com/testng)
- [testng中文官网](http://www.testng.org.cn/)
- [testng英语官网](https://testng.org/doc/index.html)

## Mockito
Mockito 主要用来辅助编写测试用例的。比如在微服务中的跨服务调用,在单个服务的单元测试中跨服务调用是不可能成功的,因为另外一个服务未启动,这是用就要使用mock 的方式,模拟服务请求返回的情形。

- 以下为一个demo,在Mockito最常用的都是spy一个实例,在该实例调用某种方法是Mock回预期的结果。
```java
@Slf4j
@AutoConfigureMockMvc
@ActiveProfiles(profiles = "test")
@SpringBootTest(classes = UnitTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class MockitoTestDemo {

@Autowired
private UnitTestService unitTestService;

@Test
public void test() {
UnitTestService spy = Mockito.spy(unitTestService);
Mockito.when(spy.testSql()).thenReturn("fadsfasdfasdf");
String result = spy.testSql();
log.info(result); // return fadsfasdfasdf

Mockito.when(spy.testParameter(Mockito.anyInt(),
Mockito.anyString(),
Mockito.any(),
Mockito.anyList(),
Mockito.anyMap()))
.thenReturn("mock success");

String res = spy.testParameter(1, "sddf", BigDecimal.ZERO, new ArrayList<>(), new HashMap<>());
log.info(res);
}
}
```

相关资料:
- [Mockito Tutorial](https://www.baeldung.com/mockito-series)

## BDDMockito
BDDMockito对比Mockito可以使用BDDMockito为各种Mockito方法提供BDD别名,代码看起来更易理解。

结合Testng的注解@BeforeClass,可以在一个测试用例类中直接把需要Mock的类方法及预期的返回值,设置好
- > Junit4中@BeforeClass,要求方法体内要为静态的。
- > BDDMockito 对于Mock类提供更为简便的表达
- > testng中对于@MockBean 需要配合使用 `@TestExecutionListeners(MockitoTestExecutionListener.class)`
```java

@Slf4j
@ActiveProfiles(profiles = "test")
@SpringBootTest(classes = UnitTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class BDDMockitoTestDemo extends AbstractTestNGSpringContextTests {

@MockBean
UnitInterface unitInterface;

@BeforeClass
public void setup() {
given(unitInterface.register(anyString())).willReturn("register success");
given(unitInterface.calculate(anyDouble())).willThrow(new IllegalArgumentException());
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void test(){
BigDecimal calculate = unitInterface.calculate(123.112d);
log.info(String.valueOf(calculate));
}
}
```

## BDDMockito
相关资料:
[Quick Guide to BDDMockito](https://www.baeldung.com/bdd-mockito)
File renamed without changes.
Loading

0 comments on commit c3e7717

Please sign in to comment.