Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:58:52+00:00 2026-06-12T16:58:52+00:00

because my project wants to start test-driven development I decided to write a small

  • 0

because my project wants to start test-driven development I decided to write a small tutorial about Junit 4 (currently JUnit 4.10 with Eclipse Juno) for my project.

Bill.java

import java.util.ArrayList;
import java.util.List;

/**
 * @author funkymonkey
 * 
 *         Class Bill can store an id and a priceList (List<Float>)
 *         - id with setter and getter
 *         - prices can be added to the priceList
 *         - getter for priceList
 *         - total amount of price in priceList can be calculated
 */

public class Bill {
   private Integer     id;       // invoice number (one for every Bill)

   private List<Float> priceList; // list will contain prices of the products

   public Bill() {
      priceList = new ArrayList<Float>();
   }

   public Bill(Integer id) {
      this.id = id;

      priceList = new ArrayList<Float>();
   }

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public List<Float> getPriceList() {
      return priceList;
   }

   public void addPrice(Float price) {
      if (price <= 0) {
         throw new IllegalArgumentException("Value is less or equal zero");
      }

      priceList.add(price);
   }

   public float getTotalPrice() {
      float totalPrice = 0;

      for (Float p : priceList) {
         totalPrice = totalPrice + p;
      }
      return totalPrice;
   }
}

BillTest.java

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.hasItems;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BillTest {

   private static Bill       jBill1;
   private static final float FLOAT_1        = (float) 1;
   private static final float FLOAT_20       = (float) 20;
   private static final float FLOAT_3P345    = (float) 3.345;
   private static final float FLOAT_0P000001 = (float) 0.000001;

   @Before
   // initialize objects before running tests
   public void setUp() throws Exception {
      jBill1 = new Bill();
   }

   @After
   // something which should be done after running a single test
   public void tearDown() throws Exception {
   }

   @Test
   // test addPrice()
   public final void testAddPrice_priceIsGreaterThanZero() {
      jBill1.addPrice(FLOAT_1);
      jBill1.addPrice(FLOAT_20);
      jBill1.addPrice(FLOAT_3P345);
      jBill1.addPrice(FLOAT_0P000001);

      // check if expected values == results

      // we are comparing lists so we are using assertEquals(expected, result)
      List<Float> expectedList = new ArrayList<Float>();
      expectedList.add(FLOAT_1);
      expectedList.add(FLOAT_20);
      expectedList.add(FLOAT_3P345);
      expectedList.add(FLOAT_0P000001);
      List<Float> resultList = jBill1.getPriceList();

      assertEquals(expectedList, resultList);

      // we are comparing arrays so we can use assertArrayEquals(expected, result)
      Object[] expectedArray = { FLOAT_1, FLOAT_20, FLOAT_3P345, FLOAT_0P000001 };
      Object[] resultArray = jBill1.getPriceList().toArray();

      assertArrayEquals(expectedArray, resultArray);

      // we are comparing strings to we can use assertEquals(expected, result)
      String expectedString = expectedList.toString();
      String resultString = jBill1.getPriceList().toString();

      assertEquals(expectedString, resultString);

      // let us compare the size of the lists using assertTrue (boolean condition)
      Integer expectedLength = expectedList.size();
      Integer resultLength = jBill1.getPriceList().size();

      assertTrue(expectedLength == resultLength);
      // or use assertTrue(expectedLength.equals(resultLength));

      // you can also use your own matchers by using assertThat(result, matcher)
      assertThat(resultList, hasItems(expectedList.toArray(new Float[expectedList.size()])));
      // or assertThat(resultList, hasItems(FLOAT_1, FLOAT_20, FLOAT_3P345, (float)
      // 0.000001));

   }

   @Test(expected = IllegalArgumentException.class)
   // test will pass if exception is thrown from addPrice()
   public final void testAddPrice_priceIsZero() {
      // this will throw the exception IllegalArgumentException
      jBill1.addPrice((float) 0);
   }

   @Test(expected = IllegalArgumentException.class)
   // test will pass if exception is thrown from addPrice()
   public final void testAddPrice_priceIsLessThanZero() {
      // this will throw the exception IllegalArgumentException
      jBill1.addPrice((float) -1);
   }

   @Test
   // test if calculating works via getTotalPrice()
   public final void testGetTotalPrice() {
      jBill1.addPrice(FLOAT_1);
      jBill1.addPrice(FLOAT_20);
      jBill1.addPrice(FLOAT_3P345);
      jBill1.addPrice(FLOAT_0P000001);

      Float expectedValue = FLOAT_1 + FLOAT_20 + FLOAT_3P345 + FLOAT_0P000001;
      Float resultValue = jBill1.getTotalPrice();

      // we are comparing float values so we can use assertEquals(expected, result)
      assertEquals(expectedValue, resultValue);
   }
}

My questions are:

  • how to explain an old fashioned coder (waterfall model) how he/she could benefit from JUnit
  • additional package which could be useful (mocking frameworks?)
  • most important methods in JUnit?
  • is the example code useful to explain somebody the functionality of junit?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:58:53+00:00Added an answer on June 12, 2026 at 4:58 pm

    In general: Don’t use magic hardcoded numbers. The better define constants so you can edit them at ONE point:

     public static final float FLOAT_1  =  1;
     public static final float FLOAT_20 = 20;
    
     public final void testAddPrice_priceIsGreaterThanZero() {
       expectedList.add((float) FLOAT_1);
       expectedList.add((float) FLOAT_20);
       List<Float> expectedList = new ArrayList<Float>();
       expectedList.add((float) FLOAT_1);
       expectedList.add((float) FLOAT_20);
       //...
     }
    

    Unittests like JUnit are designed as “one test for one responsibility”. So for your methods #getPriceList() and getPriceList().toString() etc. define all seperated Tests(could do the add in the setUp() because every test should be run independent of other tests). JUnit is fail-fast, that means after the first failed test it will beak the test-method. So you lose all tests after the first failed test, if you ceep them in one test-method.

    you could add JavaDoc to your Test-methods, to describe how they test the functionality. Random Example-Values, Expected Exceptions etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all I can't post any specific info about project because it has
I'm using NInject with NInject.Web.Mvc. To start with, I've created a simple test project
I've recently decided to write a launcher for my project. It simply downloads updated
I'm working with a small (4 person) development team on a C# project. I've
I want to use MAF in my project because I need a robust add-in
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I've recently had the need to use the managers compiler argument, because the project
Because of some apache rewrite rules in a project I'm working on, it's convenient
Specifically because of server restrictions for this project I cannot use anything like Google
I updated a project (SVN). Update failed because a file cannot be opened, althought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.