How to mock a final class with mockito

后端 未结 25 1094
日久生厌
日久生厌 2020-11-22 16:35

I have a final class, something like this:

public final class RainOnTrees{

   public void startRain(){

        // some code here
   }
}

I

25条回答
  •  囚心锁ツ
    2020-11-22 16:57

    I think you need think more in principle. Instead you final class use his interface and mock interface instead.

    For this:

     public class RainOnTrees{
    
       fun startRain():Observable{
    
            // some code here
       }
    }
    

    add

    interface iRainOnTrees{
      public void startRain():Observable
    }
    

    and mock you interface:

     @Before
        fun setUp() {
            rainService= Mockito.mock(iRainOnTrees::class.java)
    
            `when`(rainService.startRain()).thenReturn(
                just(true).delay(3, TimeUnit.SECONDS)
            )
    
        }
    

提交回复
热议问题