How to run your java program

Java Automating Testing with JUnit

  • Junit was created by programming legends Kent Beck and Erich Gamma
  • It makes it easy to implement Test-Driven Development (TDD)
  • Eclipse has JUnit support built in

JUnit is a wonderful tool for continuous testing through the development cycle. Many software engineers (For example, Kent Beck and Erich Gamma) believe that generating test cases should be done before coding  if possible.

Sometimes called Test Cases

A JUnit test case is another resource in project. As a shortcut, right-click on a Java file, then choose New Other…. if you start by clicking on the  Java file, the tools assume that Java class is the subject of your new JUnit test Case.

  • Right-click on a Java file and choose New-Other….
  • Select Java/JUnit on the right, then click Next
  • When you create a JUnit test case, name the test case (it’s a Java class) as well as the Java class tested by the test case
  • When you create the JUnit test class, that class inherits from junit.framework.TestCase. You also specify the class that you’ll be testing.
  • Eclipse gives you a list of all the public methods in your class and its superclasses
  • You decide which ones should be part of the JUnit test class
  • The JUnit tools list all the methods in the class hierarchy. You also select which methods you want to test in your test case.

In this test case, we’re calling the getGreeting() method to make sure the method returns a particular value. A real-word test case might call the same method with several different arguments, checking for the correct output each time.

  • In this example, we ask Eclipse to generate a JUnit TestCase  for the getGreeting() method

The complete testGetGreeting() method is

public void testGetGreeting() {

HelloWorld hw = new HelloWorld();

assertEquals(“Hellow, World!”,



  • We’re saying that getGreeting() should always return the string “Hello, World

Running Test Cases

When your test case is created, you can run it Run As… menu.

  • Our test case is the Java class TestHelloWorld
  • To run the class, select the test class in the package Explorer, then choose Run As – JUnit Test

Using JUnit

The idea behind test-driven development is that you test early and often. JUnit test Cases and the JUnit support build into Eclipse help you do just that.

  • You define more TestCases and TestSuites as your project progresses
  • You run the JUnit tests to make sure any changes you’ve made haven’t broken your code

Using Ant

Eclipse also has support for Ant built in.

  • Ant ( is an XML- and Java-Based built tool

–  Designed to have the same functionality as make without its quirks

  • You don’t need a tab character at the start of each line, for example

– You can extend Ant to do other tasks if you want

Within Eclipse, you can decide which targets in an Ant built file are created.

  • An Ant built file ( named built.xml by default )can define a number of targets
  • You can define which target gets built from the command line (or the Eclipse equivalent ), or let Ant figure out which one should be created

Running an Ant built file is simple.

  • Once you’ve created your built.xml file (or whatever you choose to call it), you can right-click on it and choose Run Ant…


One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *