Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unit tests to Java project in intellij IDEA?

I want to create simple java project with JUnit, so for example I'm want to write an algorithm like merge sort or some Java class and create test class so I can make unit test for that class.

I've create the project with:

File -> New -> Project -> java -> next and setup the project name and location

and I want to make the unit test for the class the I've created, and I've tried the following solotions :

  1. solution 1 from IntelliJ IDEA dosc using the light bulb to create the test class
  2. solution 2 using shortcut [ctrl + shift + t]

But I always endup with import static org.junit.Assert.*; cannot resolve symbol 'junit', I tried different unit test library end up the same way.

How to resolve this problem so I can make unit test class in this simple Java project?

like image 253
Gujarat Santana Avatar asked Apr 26 '16 23:04

Gujarat Santana


People also ask

Can We Run All unit tests inside IntelliJ IDEA?

We can run all unit tests inside the IntelliJ IDEA. IntelliJ IDEA has various unit testing frameworks like JUnit, TestNG and many more. In this section, we will understand how unit test work.

How do I create a JUnit test method in IntelliJ?

Open the corresponding JUnit test class in the editor. Place the cursor where you want a new test method to be generated. Press Alt+Insert and select Test Method from the Generate menu. By default, IntelliJ IDEA attaches the Test suffix to source class names when it automatically generates test classes.

How to create unit test in Java?

Follow the steps given below to create Unit Test: 1. Create Projects 2. In src folder, create a Java class file and enter the following code. 3. Create a New Directory with name Test to perform Unit Testing. 4. Now our Project Structure will look like this. 5. Go to File->Project Structure->Module. A new window screen will open.

How do I view test results in IntelliJ IDEA?

After IntelliJ IDEA finishes running your tests, it shows the results in the Run tool window on the Test Runner tab. For more information on how to analyze test results, refer to Explore test results.


2 Answers

You can use Gradle or Maven (my personal preference these days).

But the easiest way is to add the JUnit JAR to your project, write some tests, and execute them in IntelliJ.

  1. Go to JUnit and download version 4.12 of the JAR. Put it in a folder /test-lib in your IntelliJ project.
  2. Create a folder /src and add a package /model and a Java class Foo to it (I'll write you one).
  3. Mark /src as a source root.
  4. Create a folder /test and add a package /model and a Java class FooTest to it (I'll write that, too).
  5. Mark /test as a test source root.
  6. Right click on /test and tell IntelliJ to "Run All Tests".
  7. IntelliJ will run all the tests and present the results in the Run window.

Here's the model class:

package model;

public class Foo {
    private String value;

    public Foo(String v) { this.value = v; }

    public String toString() { return this.value; }
}

Here's the test class:

package model;

public class FooTest {

    @Test
    public void testToString() {
        String expected = "Test";
        Foo foo = new Foo(expected);
        Assert.assertEquals(expected, foo.toString());    
    }          
}
like image 144
duffymo Avatar answered Oct 08 '22 15:10

duffymo


I'm not sure this is the best solutions but I manage to build the unit test use gradle and maven. like this :

create Java project :

File -> New -> Project -> Gradle -> choose only java-> fill the groupId and ArtifactId-> choose use default gradle wrapper -> enter project name and location ->finish

and from the root of the project

right click -> Add Framework Support -> choose maven.

from there I can create the class that I want and make the unit test using the solutions from the question [ctrl + shift +t] .

like image 37
Gujarat Santana Avatar answered Oct 08 '22 16:10

Gujarat Santana