简体   繁体   English

在Mockito中模拟DAO

[英]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. 我想尝试一下Mockito,并将其添加到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: 我想模拟SQLDUMMY(创建它的目的只是为了查看我的测试是否完全可以通过,它们确实可以通过。)SQLDUMMY类如下所示:

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. 它基本上只是由吸气剂和作为现代DB存储的ArrayList的添加/删除组成。

Summary: How can I mock the SQLDUMMY class (DAO), so it is no longer a dependency for the Unit tests? 摘要:如何模拟SQLDUMMY类(DAO),因此它不再是单元测试的依赖项?

You need to read on how Mockito works. 您需要阅读Mockito的工作原理。 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 >())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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