使用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();
}
}
这是 Spock 测试失败时出现的错误:
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)
有什么想法或建议吗?我真的很喜欢 Spock 和 Mockito,我希望他们能很好地合作。非常感谢!