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

23,272

Solution 1

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());    
    }          
}

Solution 2

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] .

Share:
23,272
Gujarat Santana
Author by

Gujarat Santana

My previous code : Logger : A very simple debugging tool with colorful style. Stealer : Get all variables name and its values and write it to Go. Generator Location : Generate a new latitude and longitude form given location. And mapping the location using square shape and gives you 2D array of locations. Get a Driver : Get a driver from 2.5 millions of data less than 50 ms to specific user's location AntiBoring : Simple Android application, can you guess what image is that? Manage small team member from designers and developers. csv-reader : read csv files concurrently and find values which exist on all files. and more CV My Linkedin

Updated on July 22, 2022

Comments

  • Gujarat Santana
    Gujarat Santana almost 2 years

    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?

  • Gujarat Santana
    Gujarat Santana almost 8 years
    I'm stuck at step 6 Right click on /test and tell IntelliJ to "Run All Tests" but I manage to add the unit test library using alt + enter -> add Class path -> copy the library to my project. thanks this one works
  • duffymo
    duffymo almost 8 years
    If it's a test source, IntelliJ will run all the tests it finds.
  • Uncle Iroh
    Uncle Iroh over 3 years
    Thanks, I was missing the keyword "public" at the class level ... picky thing.