發表文章

目前顯示的是 8月, 2021的文章

Mockito 快速入門

 https://segmentfault.com/a/1190000039907700 2.1. 关键依赖 junit-jupiter-engine:5.6.2 mockito-core:3.9.0 < dependency > < groupId > org.junit.jupiter </ groupId > < artifactId > junit-jupiter-engine </ artifactId > < version > 5.6.2 </ version > < scope > test </ scope > </ dependency > < dependency > < groupId > org.mockito </ groupId > < artifactId > mockito-core </ artifactId > < version > 3.9.0 </ version > < scope > test </ scope > </ dependency > 2.2 Mockito Extension 在  JUnit5  中需要添加  mockito-junit-jupiter:3.6.28  依赖,用于支持  MockitoExtension 。 < dependency > < groupId > org.mockito </ groupId > < artifactId > mockito-junit-jupiter </ artifactId > < version > 3.6.28 </ version > < scope > test </ scope > </ dependency > 3. 待测试用例【新增用户】 业务代码 UserServiceImpl 类负责具体的业务逻...

Python

 https://ithelp.ithome.com.tw/articles/10200505 1. x / y X除以Y x // y X除以Y,只取整數解 x % y 求X除以Y的餘數 x ** y X的Y次方 2. 流程控制(if...else) 寫程式的時候,很常遇到某些情況,如:達成條件要做A,未達成要做B。 這時候我們就可以用 if...else 來做到這件事情。 假設我們有一個學生的考試成績,就可以根據成績的不同來印出評語: grade = 90 # there's no () if grade >= 90 : print( 'Excellent!' ) elif grade >= 60 : print( 'Good enough!' ) else : print( 'Loser!' ) 上述的例子中,有幾件事需要特別注意: 一個 if...else 區塊,只要其中一個條件成立,程式就會離開這個區塊。 條件不需要放 () 之中 每個條件後面記得要有 : 條件成立要做的事情,要以 縮排 的方式放在條件句底下 else if 在Python寫作 elif 3. x == y X是否等於Y x != y X是否不等於Y 4. 運算子 效果 a or b A或B其中一個條件成立就回傳True a and b A或B兩個條件都成立才回傳True not A 如果A為True,則回傳False,反之則回傳True 應用這些運算子,我們就可以寫出更多有趣的條件,如: h = 180 w = 85 grade = 80 # 身高超過175或是體重超過80,看起來就很大隻 if h > 175 or w > 80 : print( 'big dude' ) # 成績高於70但是不高於90,就是個普通學生 if grade > 70 and grade < 90 : print( 'noraml' ) 5. 序列型別 A. Range Range(start, stop, step) start:起始點 stop:停止點 step:間隔 2. Tuple Tuple可用來存放...

Mockito

使用 Mockito 生成 Mock 物件 Mockito  是一個流行 mock 框架,可以和JUnit結合起來使用。Mockito 允許你建立和配置 mock 物件。使用Mockito可以明顯的簡化對外部依賴的測試類的開發。   一般使用 Mockito 需要執行下面三步 模擬並替換測試程式碼中外部依賴。 執行測試程式碼 驗證測試程式碼是否被正確的執行 https://www.itread01.com/content/1549761155.html VVV https://kucw.github.io/blog/2020/2/spring-unit-test-mockito/ code: @Component public class UserService { @Autowired private UserDao userDao ; public User getUserById ( Integer id ) { return userDao . getUserById ( id ); } public Integer insertUser ( User user ) { return userDao . insertUser ( user ); } } Junit mockito @RunWith ( SpringRunner . class ) @SpringBootTest public class UserServiceTest { //先普通的注入一個userService bean @Autowired private UserService userService ; @Test public void getUserById () throws Exception { //普通的使用userService,他裡面會再去call userDao取得DB的data User user = userService . getUserById ( 1 ); //檢查結果 ...