TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    JUnit

    Write JUnit Test using BlueJ IDE

    Introduction

    In this post, you will learn how to write a simple JUnit test using BlueJ IDE. BlueJ is a free Java Development Environment designed for beginners. It is used by millions worldwide. ( https://bluej.org/ ).

    Let us create a simple class and write a JUnit test using BlueJ IDE.

    DVD class

    Create a DVD class with title and price. We will set the title and price for the DVD and test the method.

    DVDTest BlueJ

     

    Create a new class

    Launch BlueJ >> click on New Class button. Add/Modify the pre populated  code for the DVD class. For example as shown below:

    DVD Class BlueJ

    Code

    /**
     * Write a description of class DVD here.
     *
     * @author TestingDocs
     * @version 1.0
     */
    public class DVD
    {
     // instance variables - replace the example below with your own
     private String title;
     private double price;
    
    /**
     * Constructor for objects of class DVD
     */
     public DVD(String title, double price)
     {
     // initialise instance variables
     this.title = title;
     this.price = price;
     }
    
    /**
     * An example of a method - replace this comment with your own
     *
     * @param 
     * @return price of DVD
     */
     public double getPrice()
     {
     // put your code here
     return price;
     }
    }

    Right click on the class and choose Create Test Class. Enter DVDTest as the class name. Add a @Test method as shown below:

    DVDTest class.

    import static org.junit.Assert.*;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    /**
     * The test class DVDTest.
     *
     * @author TestingDocs
     * @version 1.0
     */
    public class DVDTest
    {
     DVD dvd;
     
     /**
     * Default constructor for test class DVDTest
     */
     public DVDTest()
     {
     }
    
    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
     @Before
     public void setUp()
     {
     dvd= new DVD("IronMan3",6.50);
     }
    
    @Test
     public void priceTest()
     {
     assertEquals(dvd.getPrice(),6.50,0.01);
     }
     
     /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
     @After
     public void tearDown()
     {
     }
    }

    Compile the classes by clicking on the compile button.

    Note that we are comparing the double floating point number with a small delta/epsilon value. ( 0.01 ). This is the tolerance value for the delta used for the comparison of the numbers.

    Run the Tests

    You can run the tests by clicking on Run tests button under Testing. Alternatively, right click the DVD class and create a test fixture to test the class.

    You can see the test result on the test result window.

    TestResult BlueJ

    Hooray, you have successfully run a JUnit Test!!  🙂

    JUnit Tutorial: https://www.testingdocs.com/junit-tutorial/

    More information on JUnit can be found on the official website: https://junit.org

    Related Posts

    Getting Started with JUnit Framework

    JUnit /

    Getting Started with JUnit Framework

    Run JUnit tests from command line

    JUnit /

    Run JUnit tests from command line

    Working with JUnit in Eclipse IDE

    JUnit /

    Working with JUnit in Eclipse IDE

    Adding JUnit5 library to a Project

    JUnit /

    Adding JUnit5 library to a Project

    Test Failure JUnit

    JUnit /

    Debug JUnit Tests using IDE

    ‹ JUnit Unit Testing Framework› @Theory and @DataPoint JUnit Annotations

    Recent Posts

    • ChatGPT Subscription Plans
    • Stellar Converter for Database
    • Stellar Log Analyzer for MySQL
    • Stellar Repair for MySQL
    • ChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI Features
    • Shaping the Future of Development: Exploring Key Trends in Software Engineering
    • Improving Java Performance with Multithreading
    • Open-source Vector Databases

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com