zoukankan      html  css  js  c++  java
  • [Unit Testing] Using Mockito Annotations

    Previously we have seen how to do Unit testing with Mockito;

    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);
        }
    }

    In this post, we are going to see, using annotation from Mockito to make testing easier:

    @RunWith(MockitoJUnitRunner.class)
    public class SomeBusinessMockTests {
    
        @Mock
        DataService mockService;
    
        @InjectMocks
        SomeBusinessImpl businessImpl;
    
        @Test
        public void testFindTheGreatestFromAllData() {
            
            when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15});
            assertEquals(24, businessImpl.findTheGreatestFromAllData());
        }
    }    

    We need to place @RunWIth for the Test class, otherwise it won't work.

    @Mock makes it easier to create a mock class.

    @InjectMock makes it easier to inject created mock into a class.

  • 相关阅读:
    数据结构 1
    MyBatis 7
    MyBatis 6
    MyBatis 5
    MaBatis 4
    MyBatis 3
    目录和文件管理
    Linux常用命令精讲
    Sentos7.4安装说明
    RIP
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10732065.html
Copyright © 2011-2022 走看看