Helpful Java: Tutorial 1 – Hello Tester!

By Farvin Gonsalves | Java

May 11

Introduction

In this Java tutorial you will learn to install Java and Eclipse, create a project, write and run a simple test.

Download and install Java

If you don’t already have Java installed, you can download it from here. You need to download the JDK and then install it.

Download and install Eclipse

Using an IDE for development makes things easier for us.I will be using Eclipse since the community supporting Eclipse is quite large. You can download the latest version of Eclipse from here. Installing Eclipse is simple, here’s a link just in case.

Create your first project

When you open Eclipse, Eclipse will ask you to select a workspace, this is where eclipse will store the project files. If you’re not happy with the default option, browse to your desired location. 

Let’s create our first project. Click on File -> New -> Java Project

Enter a project name, I have used Helpful Java and Click Finish

Now let’s examine the folder structure

We will be writing code inside the src folder but first let's create a package. Packages are used to bundle a group of related classes together. Let’s create a package called tutorial1. To create a package, right click on the src folder -> select New -> Package

Name the package tutorial1. Click on Finish.

Creating your first test

Programming in Java is done using an Object Oriented(OO) approach. All the code is written inside a class. Classes have variables and methods that allow operations. To create a test case in Java, we need to create a JUnit class. JUnit is a unit testing library that makes it easy to write unit tests.

Right click on the tutorial1 package -> New -> Junit Test Case​


Enter the Name as MyFirstTest and click on Finish. (If you see a popup about JUnit 4 not being in the build path, click OK)

​The name of any class should always start with a capital letter and conventionally CamelCase is followed. JUnit test classes should also have the word Test either in the beginning or end of the class name

Now Eclipse will create the JUnit Class with some code in it. Let us go through it.

package tutorial1;

The first line package tutorial1; means that this class belongs to the tutorial1 package.

import static org.junit.Assert.*;

import org.junit.Test;

The import statements allow us to use methods from different classes. The classes are Assert and Test.

public class MyFirstTest

public means that this class can be accessed by any other class, class is the keyword used when creating a class, MyFirstTest is the name of the class and the curly brackets {} mark the body and scope of the class

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

public void test()

This is the test method declaration where public means this method can be accessed by other classes, void means this method doesn’t return anything and test() is the name of the method. Method names are followed by () parentheses.

fail("Not yet implemented");

Eclipse has filled in some code for us in this test case. fail is a method that will always fail this test when run.

Run the test

Let’s run the test to see what happens. Right click anywhere in the class and select Run As and click on JUnit Test. 

You should see a red bar. The test has failed.

Writing your first test case

We want to test that a message says “Hello Tester”. So first let’s change the name of the test from test() to messageIsHelloTester(). Do notice method names start with first word in small caps and conventionally camelCase is followed.

Next delete the fail("Not yet implemented"); statement and type this instead

String message = "Hello Tester";

assertEquals("Message is Hello Tester", "Hello Tester", message);

Your code should look like this:​

Click on the save button and run the test (right click -> Run As -> JUnit Test). You should see a green bar. The test has passed.

Now let’s go through the code that we’ve written:

String message = "Hello Tester";

String is a data type which stores a string of characters, message is a variable name of type String which is used to store a string, and “Hello World” is the string that is stored in message.

assertEquals("Message is Hello Tester", "Hello Tester", message);

assertEquals() is a method that takes 3 parameters: message, expected result and actual result. This method checks if the expected result is equal to the actual result and displays the message if it fails.

Play Time

Now that we’ve gone through the concepts, let’s play around with the code.

Change the line String message = “Hello Tester”; to String message = “HelloTester”;

Save and Run the test. Did the test pass? I don’t think so.


Now try making this test pass without reverting our new change. (Hint: You need to change something in assertEquals("Message is Hello Tester", "Hello Tester", message);


Were you able to get the green bar? If you did, well done! If not, don’t worry, here’s the change:

assertEquals("Message is Hello Tester", "HelloTester", message);


This concludes tutorial 1 of the Helpful Java Series. We have gone through quite a lot in this tutorial. If you haven’t understood something or are having trouble with anything regarding this tutorial, please leave a comment. Your feedback is appreciated.

Follow

About the Author

Hi, I’m Farvin Gonsalves. I’m a test analyst at Planit Software Testing, UK. I’ve been a tester for more than a year now and I want to continue learning and share my experiences with you.

Leave a Comment:

(1) comment

Nidhi May 11, 2017

Thanks for information covered on the topic ,very helpful to start with Junit .

Reply
Add Your Reply

Leave a Comment: