Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Eclipse style TDD with Intellij

I just bought a copy of Intellij and I need to generate a unit test stub and put junit on the classpath and set the test to execute with the junit runner. It's trivial to do so in Eclipse.

Every instruction I read looks like this...

  1. Create a class to test
  2. Add method
  3. Create a test source root
  4. Create a test class
  5. Run test

But I do not want to create any class, or any Method until I have a Test that won't compile.

I want to layout the Class and method stubs first in a test. Once I am satisfied with the API I've expressed in the test I want to auto generate the Class and Method declarations, and then begin with the assertions.

How to do this in Intellij?

like image 310
jeremyjjbrown Avatar asked Oct 17 '13 21:10

jeremyjjbrown


People also ask

Can I run Eclipse project in Intellij?

IntelliJ IDEA automatically detects Eclipse projects located on your computer and places them in the dedicated node right on the Welcome screen. This node will be available among the recent projects until you decide to remove it. When the Eclipse project is in the node, just click it to quickly open it in the IDE.

How do I create a JUnit test case automatically in Intellij?

Right-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class. Name the new class and press Enter . Press Alt+Insert and select Test Method to generate a new test method for this class. Name the new method and press Enter .


2 Answers

Create a test folder and add a new test class, for example WhateverTest.java

Add a new method that has an @Test annotation.

@Test
public static void whatever() {
}

After you import all the required JUnit dependencies (and the ones you want), you can simply tell intellij to run the tests in the class. I don't think you have to do any special configuration beyond that.

like image 156
yamafontes Avatar answered Oct 04 '22 03:10

yamafontes


IntelliJ IDEA does not support the auto-generation of main classes from test classes. It will auto generate test classes and methods from a main class. So you could do as @yshavit recommends in his comment and create a main class skeleton and then auto generate the test class via Ctrl+Shift+T. Or as you write your test, keep an interface in sync with it. Then you can auto generate an implementation from the interface.

Otherwise, as I see it, you have three options:

  1. Open a feature request for auto-generating production classes from test classes
  2. Write your own plug-in to do this
  3. Ask the author of an existing plug-in, such as the Enso TDD focused plug-in, to add this feature.
like image 25
Javaru Avatar answered Oct 04 '22 04:10

Javaru