DevOps and Linux practice pt. 3

Continuing from the DevOps and Linux practice pt.2, this time I’m going to use Cron to run the tests automatically and update the web site based on the results.

First I had to start a new Java project in the repository, after reading a little bit I saw that running tests from console was easier if I used Maven. So I first installed Maven using sudo apt install maven and then started a Maven project with VSCode.  To set up maven correctly and run my first tests with it I followed this tutorial.

The test results will be exported to a file so that the web page could read it and update the info in it. For this part I had a lot of issues trying to read a file and pass the text to the HTML, I could not run a reader script from the HTML because ExpressJS is not made for client applications. A simple solution for this is to write the output as if it were a HTML file and then just host that new HTML. Before rewriting the new file I formatted the output with regular expressions firsts in bash.

All this work took me a lot of time to do, especially the part where I had to show the testing results in the page somehow. I tried a lot of methods I found on Internet and I ended doing a mixture of them to do something I was happy with. At the end of this week I’m going to write more about this and show the final results.

 

Chapter 4 activity

For this post we are going to show how we solved the exercises for chapter 4 using TDD. We need to add new operations like multiplying and division for the Calc.java file. The test file only has a test for the add operation as it is the only function we have in the class.

testcalc1

First of all, we need to write simple tests that describe what our new operations do. Of course, these tests should fail as we haven’t coded the functions yet.

testcalc2

Now we can start writing the functionality now, we are going to start with the subtraction as it is the first test we should pass to move to the next one. On this exercise the order we decide to write the new functions does not matter, but in more complex projects we should have a specific order to write the new functions because some tests might have multiple dependencies.

testcalc3

The next step is to write the test for the divide function. Here we have a problem, we don’t know how to implement this function because it can return a double or an integer. To follow the TDD correctly first we have to make the test pass, right now the test only ask for an integer so we will write just the code to make that correct.

testcalc4

At last, the multiply function. Here again we ask ourselves if this function should be capable of accepting float values, but it is not specified on our tests so by now we will handle only integers for the input and output.

testcalc5

 

Now lets imagine some guys from testing added new tests, now with more specific cases trying to find faults in our code. They included a test that divides two integers but the result is non integer, a test for a division by zero,  and a test with negative integers. After running the tests this happens:

testcalc6

As we can notice, only the division function is not passing the new tests. On one test it is expecting float numbers, on the other test, it is expecting us to catch an error when the divisor is zero. This means that we need a refactor to return doubles and to detect divisions by zero. This will change some of our previous tests, the first division test is working but it is expecting integers, the new test is expecting  doubles, there is an inconsistency in the tests and we should define this with the team first. The final decision is that a division should return doubles because it gives a more exact result. With that information we can go back to the tests, modify them accordingly and now start working on the refactor.

testcalc7

With this exercise we practiced TDD and refactoring. We learned how important it is to have good tests that defines the requirements for each function and that show how specific behavior should be handled. Also, it showed us how in TDD the tests are the guideline of our code and that it can show design errors or inconsistencies before we start writing the code, a huge advantage over other methodologies.

The code of the exercise is available here:

Calc.java

CalcTest.java

How to use JUnit with NetBeans

Hello to all the readers. In this post I’m going to show you how simple it is to test in Java using JUnit in NetBeans. First of all, I would like to say that this tutorial is only going to cover what we need to do to start testing simple programs for the TC3045 class in ITESM Guadalajara, that’s why we I’m going to use JUnit 4. The objective is to show how simple it is to setup the programs and start coding right away. Similar tutorials might be around the web and I’m not an expert on this subject, so if you would like to take a deeper look I recommend you going to the documentation in the official site. Everything in this tutorial is just the steps I did to have everything running in my PC(Linux). I hope you find it useful.

Step 0: Install Java

If you don’t have Java installed all you need to do is to run this commands con your console to install the JavaDevelopmentKit(JDK) in Ubuntu.

sudo add-apt-repository ppa:webupd8team/java
sudo apt install oracle-java8-installer
java -version
javac -version

Thanks to this tutorial for the help. If you are in other OS like Windows I recommend you to do the installation as any normal application, try the official java site for the JDK. Remember that sometimes you have to configure the PATH manually.

Step 1: Install NetBeans

  • Go to the official site of NetBeans and download the newest version.

1.png

As NetBeans 10 does not have an installer we must do it manually with the binary files. Download the zip file and decompress it.

2.png

3

  • Do the manual install for the bin files

For this I need you to open the terminal and go to the location of your netbeans folder. In my case I moved it to Desktop.

4

From here to do the installation insert the following commands:

sudo mv netbeans/ /opt
sudo ln -s /opt/netbeans/bin/netbeans netbeans

The first command moved the bin folder to /opt and the second command created a new command «netbeans» for the console that executes the program.

Now to open netbeans just write netbeans in your console.

5

Step 2: Create a project and write a java class

Lets star creating a new project. To avoid complications I like to create a project for the whole class and every file and exercise will be in this project.

  • Click on the «New Project» button

6

  • Select Maven and click next. We want a Java Application in Maven because it’s easier to handle dependencies.

7

  • Write the project name and choose the location. Then click «Finish».

8

Now it’s time to create a class and start coding.

  • Click on the «New File» button and create a new Java Class.

9

  • Choose the class name and click finish. I’m going to call it Operator because the class will have functions to do simple operations.

10

  • Code

Now we should have the class in out screen. I’m going to start adding simple function for sum, multiplication and a hello world.

public class Operator {
    public int sum(int a, int b){
        return a+b;
    }
    
    public int multiplication(int a, int b){
        return a*b;
    }
    
    public String helloWorld(){
        return "Hello World";
    }
}

Step 3: Create a test for that specific class

NetBeans creates automatically a testing template for the class you want, all you have to do is to modify the tests or create more.

  • Click on Tools and Create/Update Tests(You have to be on the Operator class)

11.png

**Ignore the typo in the picture 🙂

  • Deselect the options on the image(we are not going to need that), and click OK. You can change the name of the new class if you want.

12.png

Now as you can see in the new test file all the functions from Operator have a testing function. If we run the tests now it will fail because we have to remove the fail() call and modify the test.  The «assertEquals» at the end is what defines if the test passes, if the two paramenters are equal the test will pass. There are more options to create more specific tests but as I’m covering just the basics I’ll leave the code here with simple assertions that should pass.

public class OperatorTest {
    /**
     * Test of sum method, of class Operator.
     */
    @Test
    public void testSum() {
        System.out.println("sum");
        int a = 2;
        int b = 3;
        Operator instance = new Operator();
        int result = instance.sum(a, b);
        assertEquals(result, 5);
    }

    /**
     * Test of multiplication method, of class Operator.
     */
    @Test
    public void testMultiplication() {
        System.out.println("multiplication");
        int a = 5;
        int b = 3;
        Operator instance = new Operator();
        int result = instance.multiplication(a, b);
        assertEquals(result, 15);
    }

    /**
     * Test of helloWorld method, of class Operator.
     */
    @Test
    public void testHelloWorld() {
        System.out.println("helloWorld");
        Operator instance = new Operator();
        String result = instance.helloWorld();
        assertEquals(result, "Hello World");
    }
}

To run the test all you have to do is right-click on the code editor and click on «Test File» or press the buttons Ctrl+F6. The output should be something like this:

13.png

For the purpose of the class, this configurations is still not enough because NetBeans 10 uses by default JUnit5 and the book of our lecture focuses on JUnit4. The final step is to install JUnit4. Remember that I said that we are going to use Maven because is easier to handle dependencies? This is the reason, with only a few steps more everything will be ready to go.

  • Add the JUnit4 dependency.

For this you have to find the file pom.xml and open it.

14

Now inside the <dependencies> between any dependency add the next chunk of code:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

15.png

Save the file and then wait for the automatic installation.

  • Change the import of the tests.

Check the imports of your test file, it’s using the library for JUnit5. All is left to do is to use the imports from JUnit4. Remove the import lines and copy paste this ones:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

16

Now it’s all done!!

You are ready to go try your own classes and tests. If you want to learn useful tricks to do the activities check out this file, where you will learn to handle exceptions in testing. Remember that sometimes it is expected that the code fails, this tests are useful to test edge cases and avoid weird inputs.

If you want to see the sample code in GitHub here is the link to our project repository. It’s on a separate branch because is not related to the main testing, but it’s good to have samples somewhere in case we need them.

I hope this tutorial was helpful to you. Feel free to share this content or any content referenced here to help others as well. Keep learning and have fun.