JUnit 4 中的非 void 测试方法

发布于 2024-09-27 03:41:48 字数 643 浏览 11 评论 0原文

我希望 JUnit 4 测试类实现与其测试类相同的接口。这样,当接口发生变化时(我们正处于早期开发阶段,它会发生变化),编译器可以保证将相应的方法添加到测试类中。例如:

public interface Service {
  public String getFoo();
  public String getBar();
}

public class ServiceImpl implements Service {
  @Override public String getFoo() { return "FOO"; }
  @Override public String getBar() { return "BAR"; }
}

public class ServiceTest implements Service {
  @Override
  @Test
  public String getFoo() {
    //test stuff
  }

  @Override
  @Test
  public String getBar() {
    //test stuff
  }
}

当我尝试这样做时,我收到错误:“java.lang.Exception:方法 getFoo() 应该为 void”, 大概是因为测试方法必须返回 void。有人知道有什么办法解决这个问题吗?

I would like a JUnit 4 test class to implement the same interface as the class its testing. This way, as the interface changes (and it will, we're in early development), the compiler guarantees that corresponding methods are added to the test class. For example:

public interface Service {
  public String getFoo();
  public String getBar();
}

public class ServiceImpl implements Service {
  @Override public String getFoo() { return "FOO"; }
  @Override public String getBar() { return "BAR"; }
}

public class ServiceTest implements Service {
  @Override
  @Test
  public String getFoo() {
    //test stuff
  }

  @Override
  @Test
  public String getBar() {
    //test stuff
  }
}

When I try this, I get an error: "java.lang.Exception: Method getFoo() should be void",
presumably because test methods must return void. Anybody know of any way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-10-04 03:41:48

我必须承认,这是一个巧妙的技巧,尽管它不能很好地扩展到多个测试场景。

无论如何,您可以使用自定义跑步者。例如:

@RunWith(CustomRunner.class)
public class AppTest {

  @Test
  public int testApp() {
    return 0;
  }
}

public class CustomRunner extends JUnit4ClassRunner {

  public CustomRunner(Class<?> klass) throws InitializationError {
    super(klass);
  }

  protected void validate() throws InitializationError {
    // ignore
  }
}

I have to admit, it is a neat trick, though it doesn't scale well to multiple test scenarios.

Anyways, you can use custom runner. For example:

@RunWith(CustomRunner.class)
public class AppTest {

  @Test
  public int testApp() {
    return 0;
  }
}

public class CustomRunner extends JUnit4ClassRunner {

  public CustomRunner(Class<?> klass) throws InitializationError {
    super(klass);
  }

  protected void validate() throws InitializationError {
    // ignore
  }
}
伪心 2024-10-04 03:41:48

更自然的方法可能是使用代码覆盖率工具,例如 Cobertura。它与 JUnit 很好地集成,并且它向您展示了您的测试在某些情况下可能存在缺陷的情况(尽管有很多情况这样的工具无法捕获)。

A more natural way would probably be to use a code coverage tool, such as Cobertura. It integrates with JUnit nicely AND it shows you cases where your tests may be deficient in some cases (there are many cases such a tool won't catch though).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文