简体   繁体   中英

Mocking a DAO in Mockito

I'm just getting into testing of code. I have done unit tests before but haven't really isolated them. So they were more like integration test (indirectly). I want to give Mockito a try and I have added it to my Intellij IDE. But I have no idea of how to actually implement mocking at all. There are examples on their website but I just can't wrap my head around the concept of mocking. I know that one uses mocking to isolate the unit testing to ensure that the errors are in the unit itself and not in a dependency.

I wrote the following:

@Test
public void testChangeMemberReturnsTrue() throws Exception {
    Member tempMem = new Member();
    tempMem.setMemberFirstName("Swagrid");
    tempMem.setMemberLastName("McLovin");
    tempMem.setMemberID("SM666");

    SQLDUMMY.saveMember(tempMem);               //Save member to dummy DB.

    Member checkMem = new Member();
    ArrayList<Member> memArr = SQLDUMMY.getAllMembers();
    for (Member m : memArr) {                   // Look through all saved members
        if (m.equals(tempMem)) {                // If match, save to checkMem
            checkMem = m;
        }
    }
    assertTrue(tempMem.equals(checkMem));            // Make sure they are really equal.

    String newfirstname = "Darius";
    String newlastname = "DunkMaster";
    assertTrue(memhandling.changeMember(tempMem, newfirstname, newlastname));

}

And here is the actual method:

public boolean changeMember(Member mem, String n1, String n2) {
    try {
        ArrayList<Member> memArr = SQLDUMMY.getAllMembers();
        for (Member m : memArr) {
            if (m.equals(mem)) {
                m.setMemberFirstName(n1);
                m.setMemberLastName(n2);
                m.setMemberID(ensureUniqueID(m, m.getMemberID())); //Just a method call to another method in the same class to ensure ID uniqueness.
                return true;
            }
            else {
                return false;
            }
        }
    }
    catch (Exception e) {
        System.out.println("Error4.");
    }
    return false;
}

I'd like to mock the SQLDUMMY (Which I created just to see if my tests would pass at all, which they do.) The SQLDUMMY class looks like this:

public class SQLDUMMY {

private static ArrayList<Member> memberList = new ArrayList<>();
private static ArrayList<Ship> shipList = new ArrayList<>();

public static ArrayList<Member> getAllMembers() {
    return memberList;
}

public static void saveMember(Member m) {
    memberList.add(m);
}

public static void deleteMember(Member memIn) {
    memberList.remove(memIn);
}


public static void saveShip(Ship newShip) {
    shipList.add(newShip);
}

public static ArrayList<Ship> getAllShips() {
    return shipList;
}

public static void deleteShip(Ship s) {
    shipList.remove(s);
}

}

It basically just consists of getters and add/remove for the ArrayLists that act as a contemporary DB storage.

Summary: How can I mock the SQLDUMMY class (DAO), so it is no longer a dependency for the Unit tests?

You need to read on how Mockito works. The basic idea is that it extends you class and and overrides all methods and allows you to return what ever you want it too.

Syntax is :

SQLDummy sqlDummy = Mockito.mock(SQLDummy.class);
Mockito.when(sqlDummy.getAllShips()).thenReturn(new ArrayList< Ship >())

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