mardi 27 mars 2018

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.

Please let me know what am i doing wrong.

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

    @Before
    public void setUp()
    {
        MockitoAnnotations.initMocks(this);
        memberDao = Mockito.mock(MemberDao.class);

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


    public class MainClass{
    @Resource
    MemberDao memberDao;

    public String createNewId()
    {
        MembersIdDto newId = memberDao.findNext();   
        Assert.notNull(newId, "newId is empty");
        String id = 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 MainClass.createNewId() (MainClass.java:20)

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

Aucun commentaire:

Enregistrer un commentaire