Set value to mocked object but get null

后端 未结 6 664
无人共我
无人共我 2021-02-12 22:31

I have a simple class Foo to be mocked:

public class Foo {
   private String name;
   public Foo() {
   }
   public Foo(String name) {
     this.nam         


        
6条回答
  •  醉酒成梦
    2021-02-12 23:03

    I think, its better to understand as what Mockito does to your argument class and what kind of object it returns by referring to source code , Mockito Source

    General understanding is that a mocked object is a fake object ( of same type as argument provided to mock() method ) with no real method implementations.So basically, nothing happens in real life for a method call on mocked object - and that is the very purpose of creating a mocked object ( that we don't wish to call real methods ) , isn't it?

    We mock DAO layer, LDAP layer or other service dependencies because we don't want to invoke calls to actual DB or services. If we want real calls to happen, we would not create mocked objects.

    Understanding to have something real have happened after calling mockedFoo.setName("test"); OR mockedFoo.getName(); is incorrect - Nothing happens after you call a method on a mocked object and that is the purpose of mocked object which it is fulfilling.

提交回复
热议问题