Simple Mockito 验证在 JUnit 中有效,但不能在 Spock 中工作
使用Mockito示例页面中最基本的示例,我能够在JUnit中成功运行。
但是,当我在Spock中运行相同的测试时,它失败了。
JUnit/Java 版本(这经过):
import org.junit.Test;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class SimpleJunitTest
{
@Test
public void basicMockTest()
{
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
Spock/Groovy 版本(此操作失败):
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.verify
class SimpleSpockTest extends spock.lang.Specification
{
def "Basic Mock Test"()
{
given:
//mock creation
List mockedList = mock(List.class);
when:
//using mock object
mockedList.add("one");
mockedList.clear();
then:
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
以下是斯波克测试失败时我遇到的错误:
Condition not satisfied:
verify(mockedList).add("one")
| | |
| | false
| $java.util.List$$EnhancerByMockitoWithCGLIB$$172e393a@613043d2 (renderer threw
NullPointerException)
$java.util.List$$EnhancerByMockitoWithCGLIB$$172e393a@613043d2 (renderer threw
NullPointerException)
at SimpleSpockTest.Basic Mock Test(SimpleSpockTest.groovy:25)
任何想法或建议?我真的很喜欢斯波克和莫基托,希望他们能很好地合作。谢谢!