Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:可能使用Mockito进行模拟测试_Java_Unit Testing_Junit_Mocking_Mockito - Fatal编程技术网

Java:可能使用Mockito进行模拟测试

Java:可能使用Mockito进行模拟测试,java,unit-testing,junit,mocking,mockito,Java,Unit Testing,Junit,Mocking,Mockito,我想我没有正确使用验证。以下是测试: @Mock GameMaster mockGM; Player pWithMock; @Before public void setUpPlayer() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { pWithMock = new Player(mockGM); } @Test p

我想我没有正确使用
验证
。以下是测试:

@Mock GameMaster mockGM;    
Player pWithMock;

@Before
public void setUpPlayer() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    pWithMock = new Player(mockGM);
}

@Test
    public void mockDump() {
        pWithMock.testDump();
        verify(mockGM).emitRandom(); // fails
    }
以下是它调用的代码:

public boolean testDump() {
    Letter t = tiles.getRandomTile();
    return dump(t);
}

private boolean dump(Letter tile) {
            if (! gm.canTakeDump() || tiles.count() == 0) {
        return false;
    }

    tiles.remove(tile);
    gm.takeTile(tile);
    for (int i = 0; i < 3; i++) {
        tiles.addTile(gm.emitRandom()); // this is the call I want to verify
    }
    return true;
}

我要验证的调用是多层的。是否有其他方法来检查此问题?

我不确定您是否了解您在做什么。给定以下
Player
类:

public class Player {
    private final GameMaster gm;

    public Player(GameMaster gameMaster) {
        this.gm = gameMaster;
    }

    public void foo() {
        gm.bar(); // this is the call we want to verify
    }
}
public class GameMaster {
    public GameMaster() {
    }

    public void bar() {
    }
}
以及以下
GameMaster
类:

public class Player {
    private final GameMaster gm;

    public Player(GameMaster gameMaster) {
        this.gm = gameMaster;
    }

    public void foo() {
        gm.bar(); // this is the call we want to verify
    }
}
public class GameMaster {
    public GameMaster() {
    }

    public void bar() {
    }
}
我会像这样编写
Player
的测试:

import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PlayerTest {

    @Mock
    private GameMaster gm;

    @Test
    public void testFoo() {
        Player player = new Player(gm);
        player.foo();
        verify(gm).bar(); // pass
    }
}

您的测试方法中有一个错误:它缺少对
GameMaster#canTakeDump()
的必要期望。当从测试方法调用此方法时,需要返回
true
(因为它在第45行的
if
语句中使用)。

我确实不知道我在做什么。这与我发布的内容有什么不同?另外,如果GameMaster类没有不带参数的构造函数怎么办?@Rosarch这有什么不同?我不完全确定,因为您没有显示所有的代码,但是,好吧,这是可行的:)如果游戏主人没有一个没有参数的构造函数怎么办?好的,这是一个示例,但如果您需要添加此构造函数以使代码可测试,只需这样做。好的,但在您的示例中,如果
foo()
调用
odp()
,然后调用
bar()
,该怎么办。
verify(gm).bar()
是否仍能检测到它?我不确定
opd()
的功能,但如果这是您的问题,您可能需要创建完整的模拟图。
gm.canTakeDump()的+1