3 Replies - 5486 Views - Last Post: 21 August 2011 - 06:35 AM Rate Topic: -----

#1 Bairon  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 12
  • Joined: 12-May 11

question on JUnit?

Posted 10 August 2011 - 12:00 PM

Hi guys

ok so this week ive been introduced (very poorly) to something called JUnit....all i really know about it is the brief history....and im expected to work with it at some point...i've tried making some research on it but nothing really makes sense (apart from like the 5 lines wiki has on it) ya i even tried wiki...so my question is really how does it work practically? (on netbeans IDE as usual) and do you have any simple websites with basic tutorial ie what does the " @AfterClass" etc do?

Is This A Good Question/Topic? 0
  • +

Replies To: question on JUnit?

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: question on JUnit?

Posted 10 August 2011 - 02:51 PM

there is no SUPER easy explination for it other than it is a fantastic way of testing math things, or formatted things...basically anything that you give it one thing and it spits out another. I love how easy it is in netbeans to use JUnit. I believe i picked up on how to use it in netbeans help section! HA. I'll give you a bit of code that will get you started. First make you a new project in netbeans. next make a new class called MathHelper().. easiest way is to do something like this in netbeans

MathHelper() helpMe = new MathHelper();



then when the little lightbulb with red x appears to the left click on it and choose option "Create Class "MathHelper" in package ????????" <- what ever the name of your new project is called.

in the new class created paste in this code.
public class MathHelper
{
  public MathHelper()
  {
    
  }
  public int addition(int a, int B)/>
  {
    return a+b;
  }
  public int subtraction(int a, int B)/>
  {
    return a-b;
  }
  public int multiplication(int a, int B)/>
  {
    return a*b;
  }
  public int division(int a, int B)/>
  {
    return a/b;
  }
}




now in the projects side window navigate to MathHelper in your project and right click on it. Goto Tools>create JUnit Tests. If you haven't set it up yet then choose JUnit 4.x. You can usually let it choose options for you when it autogenerates the code. To be honest I don't know what all it means. But you should end up with a mess similar to this.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dichelper;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ert
 */
public class MathHelperTest
{
  public MathHelperTest()
  {
  }

  @BeforeClass
  public static void setUpClass() throws Exception
  {
  }

  @AfterClass
  public static void tearDownClass() throws Exception
  {
  }

  /**
   * Test of addition method, of class MathHelper.
   */
  @Test
  public void testAddition()
  {
    System.out.println("addition");
    int a = 0;
    int b = 0;
    MathHelper instance = new MathHelper();
    int expResult = 0;
    int result = instance.addition(a, B)/>;
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
  }

  /**
   * Test of subtraction method, of class MathHelper.
   */
  @Test
  public void testSubtraction()
  {
    System.out.println("subtraction");
    int a = 0;
    int b = 0;
    MathHelper instance = new MathHelper();
    int expResult = 0;
    int result = instance.subtraction(a, B)/>;
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
  }

  /**
   * Test of multiplication method, of class MathHelper.
   */
  @Test
  public void testMultiplication()
  {
    System.out.println("multiplication");
    int a = 0;
    int b = 0;
    MathHelper instance = new MathHelper();
    int expResult = 0;
    int result = instance.multiplication(a, B)/>;
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
  }

  /**
   * Test of division method, of class MathHelper.
   */
  @Test
  public void testDivision()
  {
    System.out.println("division");
    int a = 0;
    int b = 0;
    MathHelper instance = new MathHelper();
    int expResult = 0;
    int result = instance.division(a, B)/>;
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
  }
}




now the idea behind it is I have 4 methods (each of them just happen to take 2 integers ( a,b ).
What you are going to be doing is changing a and b to something that you can do paper and pencil or know what the expected result will be. I'll show you what i mean on the addition part. So look section of code from my JUnit test code

  @Test
  public void testAddition()
  {
    System.out.println("addition");
    int a = 0;
    int b = 0;
    MathHelper instance = new MathHelper();
    int expResult = 0;
    int result = instance.addition(a, B)/>;
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    fail("The test case is a prototype.");
  }



This is were the actual testing is done. In this case it is for my method addition. As you can see in my source code all that happens is I add a to b and return the result. So lets say a=7 and b=10 I should expect it to return 17. (int expResult = 17) next you want to comment out fail because otherwise it will say fail.

next run only that file..either by right clicking it and hitting run, or by hitting shift+f6

you should get the following output in the text Results part on the bottom.

(a progress bar should show up. in my case since i only fixed addition it says 25.00%)
1 test passed, 2 tests failed, 1 test caused an error. (0.063s)
  dichelper.MathHelper Test FAILED
    testdAddition passed (0.0s)
    testSubtraction FAILED: The test case is a prototype.
    testMultiplication FAILED: The test case is a prototype.
    testdDivision caused and ERROR: / by zero




so you could play around with that and see if you can get all 4 of them to pass. That should help give you an idea. I wish i knew what to do with the @beforeClass and @afterClass bit, but i didn't understand it..I'll probably play around wtih it now though.

*EDIT*

ok so basically it is for if you need the program to do something before you run your tests.. for instance say this program were to read a file, but the file is made before you call this class. You would have to in the beforeClass part you'd have to make that file. Or say after you did those tests that you had to close a stream of some sort, then you'd want to close it @AfterClass. to test it out you could add simple System.out.println("test before"); in those places and see were it did those things.

This post has been edited by immeraufdemhund: 10 August 2011 - 02:58 PM

Was This Post Helpful? 3
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: question on JUnit?

Posted 10 August 2011 - 04:07 PM

Nice writeup there. I think it would go well in the tutorial section. ;)
Was This Post Helpful? 0
  • +
  • -

#4 Bairon  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 12
  • Joined: 12-May 11

Re: question on JUnit?

Posted 21 August 2011 - 06:35 AM

Thank you so much! i actually understand now! you rock! why could no one just lay it out like thAT HONESTLY!? thanks so much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1