發表文章

目前顯示的是有「Testing」標籤的文章

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 ); //檢查結果 ...

Arrays - DS (Reverse array) [Easy]

  Example Return  . Sample Input 1 Copy Download Array: arr 1 4 3 2 4 1 4 3 2 Sample Output 1 2 3 4 1      static   int [] reverseArray( int [] a) {          int [] b= new   int [a.length];          int  j=a.length;        for  ( int  i= 0 ;i<a.length;i++){          b[j- 1 ]=a[i];          j=j- 1 ;       }        return  b;     }