Junit Quickstart – Live Learning Notes

I have gone through Java and Maven quickstarts. Next on the list is Junit. It is unit testing package for Java. That’s it. I just have to get myself familiarise with the syntax and setting up Junit on a Maven projec. Let’s go.

As from the last post I already have a Maven project running. It is just a getting started hello world app. But it is working. I can add more code to it and then would try to unit test it in java.

Link to last post for reference: Maven Quickstart – Live Learning Notes

Junit is already there in my pom.xml but if you don’t have it, then add the following dependency in your pom.xml

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

And run mvn compile command.

Now you have the Junit with you. My current src tree is as follows:

src
├── main
│   ├── java
│   │   └── com
│   │       └── mycompany
│   │           └── app
│   │               └── App.java
│   └── resources
└── test
    └── java
        └── com
            └── mycompany
                └── app
                    └── AppTest.java

As you may see there is already a AppTest.java. Which was generated by default when I generated the project using Maven Quickstart Archetype https://maven.apache.org/guides/getting-started/index.html.

If you don’t have the test directory, then the above tree would help you setup yours. And currently, the content of my AppTest.java is as follows.

package com.mycompany.app;

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

public class AppTest {
    @Test
    public void shouldAnswerWithTrue(){
        assertTrue( true );
    }
}

So this test case is always going to pass. To run your test, we have a maven command mvn test

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 s - in com.mycompany.app.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Now, let’s add some more code to our main App.java and try unit testing here.

Referring: https://github.com/junit-team/junit4/wiki/Getting-started

My App.jave becomes:

package com.mycompany.app;

public class App 
{
    public static void main( String[] args ) {
        System.out.println("Hello World!");
    }

    public static int evaluate(String expression) {
        int sum = 0;
        for (String summand: expression.split("\\+"))
            sum += Integer.valueOf(summand);
        return sum;
    }
}

and AppTest.java becomes

package com.mycompany.app;

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

public class AppTest
{
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }

    @Test
    public void evaluatesExpression() {
        App app = new App();
        int sum = app.evaluate("1+2+3");
        assertEquals(6, sum);
    }
}

I ran the tests with mvn test

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 s - in com.mycompany.app.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Most of the things are straightforward, let’s read more about Junit. So far it was just a quickstart with Junit. Further reading material is provide under the heading Usage and Idioms at https://junit.org/junit4/

Reading everything isn’t feasible. Better is to read selectively. For example, when you want to know about different cases of “assertions”, then you may refer them. Another case, suppose you want create “fixtures”, etc. So the idea is to read in depth the concept that you need to implement not everything.

Thanks! Have a nice day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s