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.

  • 相关阅读:
    不可或缺 Windows Native (15)
    不可或缺 Windows Native (14)
    不可或缺 Windows Native (13)
    不可或缺 Windows Native (12)
    不可或缺 Windows Native (11)
    不可或缺 Windows Native (10)
    不可或缺 Windows Native (9)
    不可或缺 Windows Native (8)
    不可或缺 Windows Native (7)
    不可或缺 Windows Native (6)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10732065.html
Copyright © 2011-2022 走看看