JUnit part II

Published in the Random EN group
members
continuation started here -> JUnit part I
JUnit part II - 1
I also want to show you how the method works Assert.fail(String)- if this method is called, the test will fail. Convenient if other methods of the class Assert cannot check for us. Let's say we need to prohibit accepting new String(“”). Let's try to do this on the constructor and disable passing name = “” || null, age = 0; Sex = null. Let's go... I added additional fields for testing
private User user;
private User user1;
private User user2;

private User userNotAdd;
private User userNotAdd1;
and changed the methodsetUp()
@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);

    userNotAdd = new User("", 0, null);
    userNotAdd1 = new User(null, 0, null);
}
and added three test methods
@Test
public void newUser_EMPTY_NAME() {
    for (User user : User.getAllUsers()){
        if (user.getName() != null && user.getName().isEmpty()) {
            Assert.fail("Попытка создания пользователя с пустым именем");
        }
    }
}

@Test
public void newUser_AGE_ZERO() {
    for (User user : User.getAllUsers()) {
        if (user.getAge() <= 0) {
            Assert.fail("Попытка создания пользователя c не допустимым возрастом");
        }
    }
}

@Test
public void newUser_SEX_NO_NULL() {
    for (User user : User.getAllUsers()) {
        if (user.getSex() == null) {
            Assert.fail("Попытка создания пользователя с указанием пола = null");
        }
    }
}
In tests, we go through the list of added users and see if there is any invalid data. We run the tests in bulk, and we see the following picture... JUnit part II - 2 You can see that our tests for the constructor are broken, and a couple more tests are also caught. Now we need to change the constructor so that it does not add a user with invalid parameters to the list of users, let's do it like this:
public User(String name, int age, Sex sex) {
    if (name != null && !name.isEmpty() && age > 0 && sex != null){
        this.name = name;
        this.age = age;
        this.sex = sex;

        if (!hasUser()) {
            countId++;
            this.id = countId;
            allUsers.put(id, this);
        }
    }
}
We run our tests and see that everything is beautiful. JUnit part II - 3 To summarize, JUnit is an excellent tool for covering your code with tests. JUnit will work even better in conjunction with Maven , when building the project, Maven will definitely run all the tests, and then build our finished project, but if the tests fail, the project will not be built, and we will know the reasons, and it will be easier to figure out where exactly a breakdown. The second advantage of JUnit is the case of Refactoring , JUnit tests will help us ensure that the logic of our program does not break. And in large projects, life is not possible without an automatic testing system, although it may be possible, but your project will move at the speed of a snail... or something like that... JUnit part II - 4 If we cover our code well with tests, then we can be sure that our client, our the product will work great. P/S I express my deep gratitude to Victor Sergeev!!! Here are links to the video lesson in which he taught us: Thank you for your attention!!! Have a good mood!!! And may God save you from Hindu code!!! ...also, if anyone is interested in digging into the source code for the article... come knocking... I’ll share the beginning here - JUnit part I
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet