- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 Spy 测试但没有成功。下面的类是一个 Sut。
public class FileManager {
public int removeFiles(String directory) {
int count = 0;
if(isDirectory(directory)) {
String[] files = findFiles(directory);
for(String file : files) {
deleteFile(file);
count++;
}
}
return count;
}
private boolean isDirectory(String directory) {
return directory.endsWith("/");
}
private String[] findFiles(String directory) {
// read files from disk.
return null;
}
private void deleteFile(String file) {
// delete a file.
return;
}
}
然后,我创建了一个如下所示的测试。
class SpyTest extends Specification {
def "Should return the number of files deleted"() {
given:
def fileManager = Spy(FileManager)
1 * fileManager.findFiles("directory/") >> { return ["file1", "file2", "file3", "file4"] }
fileManager.deleteFile(_) >> { println "deleted file."}
when:
def count = fileManager.removeFiles("directory/")
then:
count == 4
}
但是我遇到了 NullPointerException。
java.lang.NullPointerException
at example.spock.mock.FileManager.removeFiles(FileManager.java:8)
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.spockframework.mock.runtime.CglibRealMethodInvoker.respond(CglibRealMethodInvoker.java:32)
at org.spockframework.mock.runtime.MockInvocation.callRealMethod(MockInvocation.java:60)
at org.spockframework.mock.CallRealMethodResponse.respond(CallRealMethodResponse.java:29)
at org.spockframework.mock.runtime.MockController.handle(MockController.java:49)
at org.spockframework.mock.runtime.JavaMockInterceptor.intercept(JavaMockInterceptor.java:72)
at org.spockframework.mock.runtime.CglibMockInterceptorAdapter.intercept(CglibMockInterceptorAdapter.java:30)
at example.spock.mock.SpyTest.Should return the number of files deleted(SpyTest.groovy:13)
表示真正的方法被调用了。有什么原因它不起作用吗?
最佳答案
在 Spock 中,您不能模拟 java 类的私有(private)方法。查看您的 FileManager
并不清楚为什么只有 removeFiles
是公开的而其他是私有(private)的。虽然所有的方法都和文件管理有关。可能的解决方案是:
FileManager
方法。这样 Spock 就会工作,FileManager
实际上会成为一个文件管理器,而不仅仅是文件删除器FileManager
分解成不同的组件。所以你可以分别模拟这些组件并将它们注入(inject)“文件移除器”。基本上,您已经在方法级别分解了代码。但是私有(private) java 方法是 not mockable in Spock .类分解可能会产生开销,因为 FileManager
看起来与其所有操作都有一定的内聚性关于spock - 我如何使用 Spock spy ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55097304/
在特征方法中,在 when: 中指定特征 Action 。块,其结果在后续 then: 中得到测试堵塞。通常需要准备,这在 given: 中完成条款(或 setup: 或夹具方法)。包含前提条件同样有
我尝试使用 Spy 测试但没有成功。下面的类是一个 Sut。 public class FileManager { public int removeFiles(String director
我希望能够在运行一些自动化测试时记录 spock 功能名称和子句标签。这将有助于在使用 headless 浏览器进行自动化时调试测试问题,特别是 phantomjs。原因是,phantomjs 的行为
如何以编程方式跳过 Spock 框架中的测试?我知道我可以 annotate a test with @Ignore 跳过它,或使用 @IgnoreIf跳过基于环境变量等的测试。但是有没有办法运行任意
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
为简单起见,我们来看一个非常简单的类: public class TestingClass { public void method1(){ System.out.printl
Spock 只允许从 where 块访问静态变量。 是否有任何解决方法可以在 where 块中使用哪些实例变量? 最佳答案 您可以使用 @Shared 注释实例变量,见 http://spockfra
我正在使用 Spock 框架进行测试,一切都很好,直到今天;我不知道发生了什么。 Intellij 说“配置 Groovy sdk”所以我下载了 groovy sdk 2.4.9 并配置了它,但是在我
我正在为 grails 2.1.1 应用程序的一组现有测试添加第一个 spock 集成测试。使用以下方法运行时,测试运行和测试通过: grails test-app integration:spock
我过去曾在其他项目中使用旧版本的 robolectric 使用 robospock 和 electricspock 对 robolectric 进行过 spock 测试。我的新项目使用 robolec
我正在使用 Maven Surefire 插件运行一组 Spock 测试作为集成测试用例。我知道我们可以使用 @Shared 关键字在单个文件中跨规范的固定装置共享资源。 但是,是否可以在不同的规范文
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: def "findAll() Expected"(){ given: "The UR
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: @FailsWith(java.lang.AssertionError.class) def
我正在为我的插件创建 Spock 测试 project-plugin我的主要项目名称是 main-project正在使用 project-plugin作为插件。因此,当我为我的插件创建 Spock 测
在JUnit 3中,我可以这样获得当前正在运行的测试的名称: public class MyTest extends TestCase { public void testSomething(
我有一些类似Java的东西: public interface EventBus{ void fireEvent(GwtEvent event); } public class SaveCom
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
我遇到的问题是当我尝试在 then 中验证时阻止已抛出异常,并且已进行模拟调用。 看看下面的设置: class B { def b(A a) { a.a() } } c
我是一名优秀的程序员,十分优秀!