java - Final method mocking -


i need mock class final method using mockito. have wrote

@test public void test() {     b b = mock(b.class);     doreturn("bar called").when(b).bar();        assertequals("must \"overrided\"", "bar called", b.bar());     //bla-bla }   class b {     public final string bar() {         return "fail";     } } 

but fails. tried "hack" , works.

   @test    public void hacktest() {         class newb extends b {             public string barfortest() {                 return bar();             }         }         newb b = mock(newb.class);         doreturn("bar called").when(b).barfortest();         assertequals("must \"overrided\"", "bar called", b.barfortest());     } 

it works, "smells".

so, right way?

thanks.

there no support mocking final methods in mockito.

as jon skeet commented should looking way avoid dependency on final method. said, there ways out through bytecode manipulation (e.g. powermock)

a comparison between mockito , powermock explain things in detail.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -