JUnit :: or how to love the JavaRush validator
Create an object
And so, in order to test something, we first need a test object. We have a task ahead of us.- We need an object that will store information about the User.
- Id - must be counted in the order in which the new user was added.
- Username.
- His age.
- Gender (male/female)
-
It is necessary to provide storage of a list of users.
-
The class must be able to.
- Generate a list of all users.
- Generate a list of users by gender (MALE/FEMALE).
- Return the number of users in the general list, and calculate the number based on the user's gender.
- Calculate the total amount by user age, and also take into account gender.
- Calculate the average age, both overall and by gender.
User
that will contain fields:
private int id;
private String name;
private int age;
private Sex sex;
This is enough to store user data, let's see what else is needed for the task. We need to somehow store all users, let's make a static field in our class allUsers
, I think it's ok if it isMap<Integer, User>
private static Map<Integer, User> allUsers;
We also somehow need to assign a sequence number to users, let's create a static counter field, which, when creating a new user, will assign a sequence number Id
to the user.
private static int countId = 0;
So, we seem to have sorted out the fields, let’s write a constructor for our object, and getters for the fields id
, name
, age
, sex
. There is nothing complicated with heterae, let’s ask IDEA for help , she will never refuse, and we’ll make the constructor a little tricky. The designer will be able to. Initialize the fields, check whether there is such an object in allUsers
, if there is no such object, then increase our counter countId++
and add it to the list of all users. And also initialize the field allUsers
if it has not yet been initialized. For the convenience of searching for identical objects, we will redefine the methods equals()
and hashCode()
, again we will ask for help from our beloved IDEA and compare by fields name
, age
, sex
. Plus, let's create a private method hasUser()
that will check whether such an object is in the list.
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age &&
Objects.equals(name, user.name) &&
sex == user.sex;
}
@Override
public int hashCode() {
return Objects.hash(name, age, sex);
}
In the end, I ended up with a designer like this.
public User(String name, int age, Sex sex) {
if (allUsers == null){
allUsers = new HashMap<>();
}
this.name = name;
this.age = age;
this.sex = sex;
if (!hasUser()){
countId++;
this.id = countId;
allUsers.put(id, this);
}
}
and a private helper method
private boolean hasUser(){
for (User user : allUsers.values()){
if (user.equals(this) && user.hashCode() == this.hashCode()){
return true;
}
}
return false;
}
and also redefinetoString()
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
Now it's time to implement the logic of the required methods. Since the logic will mainly work with static fields, we will also make the methods static; they are not needed for objects.
- Generate a list of all users.
- Generate a list of users by gender (MALE/FEMALE).
-
Return the number of users in the general list, and calculate the number based on the user's gender.
public static int getHowManyUsers(){ return allUsers.size(); } public static int getHowManyUsers(Sex sex){ return getAllUsers(sex).size(); }
-
Calculate the total amount by user age, and also take into account gender. Let's create methods for this task.
public static int getAllAgeUsers(){ int countAge = 0; for (User user : allUsers.values()){ countAge += user.age; } return countAge; } public static int getAllAgeUsers(Sex sex){ int countAge = 0; for (User user : getAllUsers(sex)){ countAge += user.age; } return countAge; }
-
Calculate the average age, both overall and by gender.
public static int getAverageAgeOfAllUsers(){ return getAllAgeUsers() / getHowManyUsers(); } public static int getAverageAgeOfAllUsers(Sex sex){ return getAllAgeUsers(sex) / getHowManyUsers(sex); }
Great, we have described the required object and its behavior. Now we can move on to JUnit , but first I’ll show you what a simple test will look like if we do it in main .
public static void main(String[] args) { new User("Eugene", 35, Sex.MALE); new User("Marina", 34, Sex.FEMALE); new User("Alina", 7, Sex.FEMALE); System.out.println("All users:"); User.getAllUsers().forEach(System.out::println); System.out.println("All users: MALE"); User.getAllUsers(Sex.MALE).forEach(System.out::println); System.out.println("All users: FEMALE"); User.getAllUsers(Sex.FEMALE).forEach(System.out::println); System.out.println("================================================"); System.out.println(" all users: " + User.getHowManyUsers()); System.out.println(" all MALE users: " + User.getHowManyUsers(Sex.MALE)); System.out.println("all FEMALE users: " + User.getHowManyUsers(Sex.FEMALE)); System.out.println("================================================"); System.out.println(" total age of all users: " + User.getAllAgeUsers()); System.out.println(" total age of all MALE users: " + User.getAllAgeUsers(Sex.MALE)); System.out.println("total age of all FEMALE users: " + User.getAllAgeUsers(Sex.FEMALE)); System.out.println("================================================"); System.out.println(" average age of all users: " + User.getAverageAgeOfAllUsers()); System.out.println(" average age of all MALE users: " + User.getAverageAgeOfAllUsers(Sex.MALE)); System.out.println("average age of all FEMALE users: " + User.getAverageAgeOfAllUsers(Sex.FEMALE)); System.out.println("================================================"); }
The output to the console will look something like this, and then we’ll compare whether we got normal operation. We can, of course, go deeper, write comparison logic, and see what our calculation says, even though we will not be sure that we were able to provide for everything.
//output Все пользователи: User{id=1, name='Eugene', age=35, sex=MALE} User{id=2, name='Marina', age=34, sex=FEMALE} User{id=3, name='Alina', age=7, sex=FEMALE} Все пользователи: MALE User{id=1, name='Eugene', age=35, sex=MALE} Все пользователи: FEMALE User{id=2, name='Marina', age=34, sex=FEMALE} User{id=3, name='Alina', age=7, sex=FEMALE} ================================================ всех пользователей: 3 всех пользователей MALE: 1 всех пользователей FEMALE: 2 ================================================ общий возраст всех пользователей: 76 общий возраст всех пользователей MALE: 35 общий возраст всех пользователей FEMALE: 41 ================================================ средний возраст всех пользователей: 25 средний возраст всех пользователей MALE: 35 средний возраст всех пользователей FEMALE: 20 ================================================ Process finished with exit code 0
We are not happy with this outcome, down with main tests , we need JUnit .
Points a and b can be handled well by a method getAllUsers()
that will return a list of all User
, and an overloaded method getAllUsers(Sex sex)
that will return a list, depending on the passed parameter Sex
.
public static List<User> getAllUsers(){
return new ArrayList<>(allUsers.values());
}
public static List<User> getAllUsers(Sex sex){
List<User> listAllUsers = new ArrayList<>();
for (User user : allUsers.values()){
if (user.sex == sex){
listAllUsers.add(user);
}
}
return listAllUsers;
}
How to connect JUnit to a project
The question arises how to connect it to the project. For those in the know, I won’t take the option with Maven , as this is a completely different story. ;) Open the project structure Ctrl + Alt + Shift + S -> Libraries -> click + (New Project Library) -> select from Maven,


User
-> press Alt + Enter -> select create Test. We should see a window in which we need to select the JUnit4 library -> select the methods that we are going to test -> OK 
UserTest
, this is the class in which we will cover our code with tests. Let's get started:
Our first @Test
Let's create our first @Test methodgetAllUsers()
- this is a method that should return all users. The test will look something like this:
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
Here we create several test users -> create a list expected
in which we will place the users whose method will return to us getAllUsers()
-> create a list actual
in which we will place the users whose we assume that the getAllUsers()
Assert.assertEquals(actual, expected) method will be used and we will pass the lists, inspected and current, to it. This method will test the objects in the provided lists and return the test result. The method will compare all fields of objects, even going through the fields of parents if there is inheritance. Let's run the first test... 
actual
.
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
//actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
we run the test and see the following: 
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(new User("User1", 1, Sex.MALE));
actual.add(new User("User2", 2, Sex.FEMALE));
actual.add(new User("User3", 3, Sex.MALE));
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
This is what will be in the console: 

main
can this happen? - No. JUnit : main = 2 : 0 Well, let's move on, we still have a bunch of methods that need to be covered with tests), but wait, it won’t be bad to check if the method will getAllUsers()
return to us, because that’s what we do with JavaRushnull
tasks caught by the validator). Let’s do this, it’s a matter of three kopecks...
@Test
public void getAllUsers_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
Yes, yes, this is approximately how the validator catches our null
;) Now let’s run this test and see what it shows us. And it will show an error, how???? How could a test error be made here))) 
allUsers
we initialized the field in the constructor, which means that when calling the method getAllUsers()
, we will refer to an object that has not yet been initialized. Let's edit it, remove the initialization from the constructor, and do it when declaring the field.
private static Map<Integer, User> allUsers = new HashMap<>();
public User(String name, int age, Sex sex) {
this.name = name;
this.age = age;
this.sex = sex;
if (!hasUser()) {
countId++;
this.id = countId;
allUsers.put(id, this);
}
}
Let's run the test, now everything is fine. 
package user;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class UserTest {
@Test
public void getAllUsers() {
// create test data
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
//create an expected list and fill it with the data of our method
List<User> expected = User.getAllUsers();
//create actual list put data in it for comparison
//what we expect the method to return
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
//run the test if the list expected and actual are not equal
//the test will fail, read the test results in the console
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
List<User> expected = User.getAllUsers(Sex.MALE);
List<User> actual = new ArrayList<>();
actual.add(user);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_MALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.MALE);
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
List<User> expected = User.getAllUsers(Sex.FEMALE);
List<User> actual = new ArrayList<>();
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_FEMALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.FEMALE);
Assert.assertNotNull(expected);
}
@Test
public void getHowManyUsers() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers();
int actual = 3;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers(Sex.MALE);
int actual = 1;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getHowManyUsers(Sex.FEMALE);
int actual = 2;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers();
int actual = 35 + 34 + 7;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_MALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers(Sex.MALE);
int actual = 35;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_FEMALE() {
User user = new User("Eugene", 35, Sex.MALE);
User user1 = new User("Marina", 34, Sex.FEMALE);
User user2 = new User("Alina", 7, Sex.FEMALE);
int expected = User.getAllAgeUsers(Sex.FEMALE);
int actual = 34 + 7;
Assert.assertEquals(expected, actual);
}
}
Yes, it turned out to be not small, but what will happen when working with large projects. What can be reduced here? After evaluating everything, you can see that we create test data in each test, and here annotations come to our aid. Let's take @Before
- The annotation @Before
indicates that the method will be executed before each tested method @Test
. This is what our test class with annotation will now look like @Before
:
package user;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class UserTest {
private User user;
private User user1;
private User user2;
@Before
public void setUp() throws Exception {
user = new User("Eugene", 35, Sex.MALE);
user1 = new User("Marina", 34, Sex.FEMALE);
user2 = new User("Alina", 7, Sex.FEMALE);
}
@Test
public void getAllUsers() {
List<User> expected = User.getAllUsers();
List<User> actual = new ArrayList<>();
actual.add(user);
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_NO_NULL() {
List<User> expected = User.getAllUsers();
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_MALE() {
List<User> expected = User.getAllUsers(Sex.MALE);
List<User> actual = new ArrayList<>();
actual.add(user);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_MALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.MALE);
Assert.assertNotNull(expected);
}
@Test
public void getAllUsers_FEMALE() {
List<User> expected = User.getAllUsers(Sex.FEMALE);
List<User> actual = new ArrayList<>();
actual.add(user1);
actual.add(user2);
Assert.assertEquals(expected, actual);
}
@Test
public void getAllUsers_FEMALE_NO_NULL() {
//add check for null
List<User> expected = User.getAllUsers(Sex.FEMALE);
Assert.assertNotNull(expected);
}
@Test
public void getHowManyUsers() {
int expected = User.getHowManyUsers();
int actual = 3;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_MALE() {
int expected = User.getHowManyUsers(Sex.MALE);
int actual = 1;
Assert.assertEquals(expected, actual);
}
@Test
public void getHowManyUsers_FEMALE() {
int expected = User.getHowManyUsers(Sex.FEMALE);
int actual = 2;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers() {
int expected = User.getAllAgeUsers();
int actual = 35 + 34 + 7;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_MALE() {
int expected = User.getAllAgeUsers(Sex.MALE);
int actual = 35;
Assert.assertEquals(expected, actual);
}
@Test
public void getAllAgeUsers_FEMALE() {
int expected = User.getAllAgeUsers(Sex.FEMALE);
int actual = 34 + 7;
Assert.assertEquals(expected, actual);
}
}
Well, how about you, it’s already more fun and easier to read ;) Here’s a list of annotations for JUnit, it’s definitely easier to live with them.
@Test – определяет что метод method() является тестовым.
@Before – указывает на то, что метод будет выполнятся перед каждым тестируемым методом @Test.
@After – указывает на то что метод будет выполнятся после каждого тестируемого метода @Test
@BeforeClass – указывает на то, что метод будет выполнятся в начале всех тестов,
а точней в момент запуска тестов(перед всеми тестами @Test).
@AfterClass – указывает на то, что метод будет выполнятся после всех тестов.
@Ignore – говорит, что метод будет проигнорирован в момент проведения тестирования.
(expected = Exception.class) – указывает на то, что в данном тестовом методе
вы преднамеренно ожидаете Exception.
(timeout = 100) – указывает, что тестируемый метод не должен занимать больше чем 100 миллисекунд.
Main class methods Assert
for checking:
fail(String) – указывает на то что бы тестовый метод завалился при этом выводя текстовое сообщение.
assertTrue([message], boolean condition) – проверяет, что логическое condition истинно.
assertsEquals([String message], expected, actual) – проверяет, что два значения совпадают.
Примечание: для массивов проверяются ссылки, а не содержание массивов.
assertNull([message], object) – проверяет, что an object является пустым null.
assertNotNull([message], object) – проверяет, что an object не является пустым null.
assertSame([String], expected, actual) – проверяет, что обе переменные относятся к одному an objectу.
assertNotSame([String], expected, actual) – проверяет, что обе переменные относятся к разным an objectм.
This is how we can add JUnit 4.12 dependency in Maven
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
continued here -> JUnit part II
GO TO FULL VERSION