Using Mockito with TestNG

后端 未结 3 1120
陌清茗
陌清茗 2021-02-09 06:33

I took a working test written for JUnit using Mockito and tried to adapt it to work with TestNG but oddly using TestNG only one test will work.

I think it is somehow rel

3条回答
  •  无人共我
    2021-02-09 07:24

    You can also use MockitoTestNGListener it is similar to JUnit MockitoJUnitRunner or MockitoExtension.

    Example of code:

    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.testng.MockitoTestNGListener;
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    
    import java.util.Map;
    
    @Listeners(MockitoTestNGListener.class)
    public class MyTest {
    
        @Mock
        Map map;
    
        @InjectMocks
        SomeType someType;
    
        @Test
        void test() {
            // ...
        }
    }
    

提交回复
热议问题