简体   繁体   中英

Mockito mocked method is returning NULL

I am using Mockito and have tried to mock the below test class. Here the main class method createNewId() is getting the object by hitting dao class 'memberDao.findNext()'. I am trying to mock 'memberDao.findNext()' and return the object as shown in below code but it is returning as NULL.

ALSO how to write Test for void method which is "memberDao.delete(newId.getId());" Need to implement this after this line "when(memberDao.findNext()).thenReturn(id);"

Please let me know what am i doing wrong.

@RunWith(MockitoJUnitRunner.class)
public class MemberTest
{
    @InjectMocks
    private Member member;
    @Mock
    private MemberDao memberDao;

    @Test
    public void createId() throws Exception
    {
        MembersIdDto id = new MembersIdDto();
        id.setId("967405286");
        when(memberDao.findNext()).thenReturn(id);
        verify(member).createNewId().contains("967405286");
    }


    public class Member {
    @Resource
    MemberDao memberDao;

    public String createNewId()
    {
        MembersIdDto newId = memberDao.findNext();   
        Assert.notNull(newId, "newId is empty");
        String id = newId.getId();
        memberDao.delete(newId.getId());
        return id;
    }
    }

memberDao.findNext() is the line i am trying to mock.

Error is : java.lang.IllegalArgumentException: newId is empty

at org.springframework.util.Assert.notNull(Assert.java:134)
at Member.createNewId() (Member.java:20)

// Line 20 is "Assert.notNull(newId, "newId is empty");"

A working example of your requirement could be:

@RunWith(MockitoJUnitRunner.class)
public class MemberTest {
    @InjectMocks
    private Member member;
    @Mock
    private MemberDao memberDao;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void createId() throws Exception {
        MembersIdDto dto = new MembersIdDto();
        dto.setId("967405286");
        when(memberDao.findNext()).thenReturn(dto);

        assertThat(member.createNewId()).isEqualTo("967405286");
    }
}

…with the classes-under-test…

public class Member {
    @Resource
    MemberDao memberDao;

    public String createNewId() {
        return memberDao.findNext().getId();
    }
}

…and…

public class MemberDao {
    public MembersIdDto findNext() {
        return null; // or whatever
    }
}

…and…

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class MembersIdDto {
    private String id;
}

By the way, I use the AssertJ assertion framework and with member.createNewId() you have now a real call on production code.

You're creating two instances of MemberDao. First MockitoJUnitRunner creates an instance, assigns it to the field memberDao and injects that instance into the Member object. Afterwards in the method setUp you create a new instance of the DAO and assign it to the field memberDao . Therefore the field memberDao is no longer the same as the Member's DAO. While you define behaviour on the field, the Member object is still using the first DAO that has no behaviour defined. Therefore memberDao.findNext() in Member#createNewId returns null .

Fortunately the solution is very simple: Delete the setUp method in your test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM