Set value to mocked object but get null

后端 未结 6 665
无人共我
无人共我 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 22:53

    To solve your problem of knowing whether the setName() method was called in your code with the specified value use the Mockito verify method.

    For example:

    verify(mockedFoo, times(1)).setName("test");
    

    Will verify that the mockedFoo.setName() method with the String parameter "test" was called exactly once in the code under test.

    0 讨论(0)
  • 2021-02-12 22:57

    By default mockito has no behavior, on any methods in the mocked object.

    You should do something like this:

    Foo mockedFoo = Mockito.mock(Foo.class);
    when(mockedFoo.getName()).thenReturn("someName");
    String name = mockedFoo.getName();
    

    edit: as mentioned by Jon Skeet, you should not need to mock things that can just be tested in a normal fashion.

    0 讨论(0)
  • 2021-02-12 22:57

    You need to call real methods on the mocked objects. use @Spy

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-12 23:06

    Well yes - the actual code of Foo doesn't matter, because you're mocking it... and Mockito doesn't know there's meant to be a relationship between setName and getName. It doesn't assume that it should store the argument to setName and return it when getName is called... it could do that, but it doesn't as far as I'm aware. The mock provided by Mockito just allows you to specify what happens when methods are called on it, and check what was called later on. Instead of calling setName, you could mock a call to getName() and specify what it should return...

    ... or you could just use Foo directly instead of mocking it. Don't think you have to mock everything in your tests. Just mock (or fake) things that are awkward when you're using the real class, e.g. because it uses the file system or network.

    0 讨论(0)
  • 2021-02-12 23:07

    Somewhat this kind of approach is to used. I have written for EasyMock but with Mockito it can also be used as same.

    String nameMock = createNiceMock(String.class);
    Foo MockedFoo = new Foo();
    Foo.setName(nameMock);
    
    EasyMock.expect(Foo.getName()).andReturn(nameMock);
    
    0 讨论(0)
提交回复
热议问题