zoukankan      html  css  js  c++  java
  • [Spring Boot] Introduce to Mockito

    We have the implemetion: 

    @SpringBootApplication
    public class MockitoDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MockitoDemoApplication.class, args);
        }
    
    }
    
    public class SomeBusinessImpl {
        private DataService dataService;
    
        public SomeBusinessImpl(DataService dataService ){
            super();
            this.dataService = dataService;
        }
    
        public int findTheGreatestFromAllData() {
            int[] data = dataService.retieveAllData();
            int greatest = Integer.MIN_VALUE;
            for (int value : data) {
    
                if (value > greatest) {
                    greatest = value;
                }
            }
            return greatest;
        }
    }
    
    
    public interface DataService {
        int[] retieveAllData();
    }

    And we want to test agaisnst it:

    import org.junit.Test;
    
    import static org.junit.Assert.assertEquals;
    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    
    public class SomeBusinessMockTests {
    
        @Test
        public void testFindTheGreatestFromAllData() {
            DataService mockService = mock(DataService.class);
            when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15});
    
            SomeBusinessImpl businessImpl = new SomeBusinessImpl(mockService);
            int result = businessImpl.findTheGreatestFromAllData();
            assertEquals(24, result);
        }
    }
  • 相关阅读:
    BlangenOA项目总结
    ==和Equals与值类型和引用类型
    SQL Server索引
    Html5 之拖动
    Html5 之过渡
    Html 之登录界面
    Html 之进度条
    GUI 之密码框
    GUI 之文本框
    GUI 之列表框
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10725146.html
Copyright © 2011-2022 走看看