How to create junit test case in intellij

What you will learn here about Junit

  • How to create junit test case in intellij

How to create junit test case in intellij

Please follow the following steps to know how to create junit test case in intellij
1)First create a maven project in intellij

2)First add dependencies of Junit dependency in POM.xml which is shown below

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

pom dependency lombok and junit

3)Create a class whose test case you want to create which is shown below

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {

    String firstName;
    String lastName;

    public String toUpper(String name){
        return name.toUpperCase();
    }

    public Person() {
    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

junit test case example intellij

4)Now please follow following steps to create Junit test case class for your java file which is shown below
right click in java file -> Generate -> Test -> Choose Testing library as JUnit4 -> click on OK

IntelliJ how to create Test cases in Java

5)Now write your test case which is shown below
How to create Test cases in Java IntelliJ

import org.junit.Assert;
import org.junit.Test;

public class PersonTest {

    @Test
    public void toUppertest(){
        Person person=new Person("john","dow");
        Assert.assertEquals("JOHN",person.toUpper(person.getFirstName()));
        Assert.assertEquals("DOW",person.toUpper(person.getLastName()));
    }
}

You may also like...