为 Eclipse 中现有类中的新方法生成 JUnit 存根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4024942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:14:04  来源:igfitidea点击:

Generating JUnit stubs for new methods in existing class in Eclipse

eclipsejunit

提问by Dan Coates

This question is tangentially related to How can I create JUnit stub methods in eclipse?but it doesn't answer my specific question.

这个问题与如何在 eclipse 中创建 JUnit 存根方法有切线相关但它没有回答我的具体问题。

Assuming you have an existing JUnit test case class, if you add a method to the target class (or choose to test a previously untested method), is there a way to tell Eclipse to generate the stub for the "new" method(s) in the existing TestCase without creating a new test case class?

假设您有一个现有的 JUnit 测试用例类,如果您向目标类添加一个方法(或选择测试以前未测试的方法),是否有办法告诉 Eclipse 为“新”方法生成存根在现有的 TestCase 中而不创建新的测试用例类?

回答by Chelmite

  1. Install the MoreUnit extension from the MarketPlace.
  2. Open the package hierarchy panel.
  3. Navigate down to the class that you've modified.
  4. Right click on the new method.
  5. Select "Generate Test". The generated stub will appear in your ClassTest file.
  1. 从 MarketPlace 安装 MoreUnit 扩展。
  2. 打开包层次结构面板。
  3. 向下导航到您修改过的课程。
  4. 右键单击新方法。
  5. 选择“生成测试”。生成的存根将出现在您的 ClassTest 文件中。

回答by DaveM

My solution.

我的解决方案。

I simply go through the standard 'create JUnit test case'

我只是通过标准的“创建 JUnit 测试用例”

Select file to test. -> New Junit test case

选择要测试的文件。-> 新的 Junit 测试用例

Go through the normal process in creating the test case, but only select those that you want new stubs for.

执行创建测试用例的正常过程,但只选择您想要新存根的那些。

The file is created with the stubs, which I now copy into the existing test case file. Delete the newly created test file class.

该文件是用存根创建的,我现在将其复制到现有的测试用例文件中。删除新创建的测试文件类。

It's not the most efficient, but quicker than 'thick fingering' when you create them manually.

当您手动创建它们时,这不是最有效的,但比“粗指法”更快。

David

大卫

回答by khmarbaise

The usual working cycle with unit tests is to create the test case first

单元测试通常的工作周期是首先创建测试用例

public void test_new_method() {
    ClassUnderTest x = new ClassUnderTest();
    x.NewMethod();
}

At that point Eclipse will give you a warning that the method "NewMethod" does not exist. Than you simply select a "Quick Fix" (Ctrl-1 or Ctrl-2 i'm not sure) to create the missing method in the class.

那时 Eclipse 会警告您方法“NewMethod”不存在。比您简单地选择一个“快速修复”(Ctrl-1 或 Ctrl-2 我不确定)来创建类中缺少的方法。