Generating JUnit stubs for new methods in existing class in Eclipse

╄→гoц情女王★ 提交于 2019-12-20 11:20:31

问题


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

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?


回答1:


  1. Open the package hierarchy panel.
  2. Navigate down to the class that you've modified.
  3. Right click on the new method.
  4. Select "Generate Test". The generated stub will appear in your ClassTest file.



回答2:


My solution.

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

Select file to test. -> New Junit test case

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




回答3:


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.



来源:https://stackoverflow.com/questions/4024942/generating-junit-stubs-for-new-methods-in-existing-class-in-eclipse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!