Agapito Gallart Bernat
Agapito Gallart Bernat

Reputation: 363

Generate a new JUnit test after implementing some methods of a class under test

I am newbie in IntelliJ, I used to use NetBeans. My situation is the following:

  1. I create the class and the corresponded test.

  2. I create some method in the class.

Is there is any feature in IntelliJ for refactoring the test, so that the new method/function created will generate its corresponding test function in the test class? I stress that the test file is already generated.

Edit1: My aim is updating the current test, not generating a new one. I am looking to a feature which implements the new test function into the current test. Here is the situation with snippet code:

src/pack/Foo.java

package pack

class Foo {
     \\ variables
     public Foo (arguments){
         \\ variable initialization
     }

     private void metho1(){ ... }

Test/pack/FooTest.groovy

package pack

class FooTest {
     private static Foo tmp = new Foo (arguments)

     void testmetho1(){ ... }

src/pack/Foo.java

package pack

class Foo {
     \\ variables
     public Foo (arguments){
         \\ variable initialization
     }

     private void metho1(){ ... }
     private void metho2(){ ... }
     private void metho3(){ ... }

Test/pack/FooTest.groovy

package pack

class FooTest {
     private static Foo tmp = new Foo (arguments)

     void testmetho1(){ ... }
     void testmetho1(){ ... }
     void testmetho1(){ ... }

Upvotes: 2

Views: 314

Answers (2)

CJDood
CJDood

Reputation: 371

I put my cursor on the new method name

press {Generate... (default Alt+Insert I think)}

Select 'Tests...'

select my method(s) I want to generate test scaffolding for

have it use the existing class.

Upvotes: 2

CrazyCoder
CrazyCoder

Reputation: 401905

Please check the documentation:

Create test methods

To create stub test methods in JUnit test classes, you can use the IntelliJ IDEA code generation feature.

  1. Open the corresponding JUnit test class in the editor.
  2. Place the cursor where you want a new test method to be generated.
  3. Press Alt+Insert and select Test Method from the Generate menu.

Upvotes: 1

Related Questions