6 Replies - 1461 Views - Last Post: 13 November 2012 - 03:41 PM Rate Topic: ***** 1 Votes

#1 sturgis23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 12

help using JUnit testing

Posted 13 November 2012 - 02:48 PM

for now I am just trying to figure out how to test one value, all the examples I have found online have not been helful at all. So far I have this from an example my teacher gave us. if you can point out what i need to do with my "actual string" or what i am missing i would appreciate it. Thank You!
import junit.framework.TestCase;

/**
 * A JUnit test case class.
 * Every method starting with the word "test" will be called when running   (????)
 * the test with JUnit.
 */
public class testRoman extends TestCase {
  
  /**
   * A test method.
   * (Replace "X" with a name describing the test.  You may write as
   * many "testSomething" methods in this class as you wish, and each
   * one will be called when running JUnit over this class.)
   */


  //1975, 1988 and 2007  the first few numbers to test
  public void test1() {
    Roman1 roman = new Roman1();
    String expected =  "MCMDCCV";
    String actual = roman.setYearDecimal(1975);    //?????
    assertEquals("Expected Result", expected, roman); 
  }
  
}


//and my class i need to test




public class Roman1
{
  private int decYear;
  private String romanNumYear = "";
  
  //constructor no parameter
  public Roman1()
  {
    decYear = 0;
    romanNumYear = "";
  }
  //constructor parameter: int
  public Roman1(int decYear)  // int to string
  {
    // Calls setYearDecimal() method passing the decimal year parameter as the argument
    setYearDecimal(decYear);
  }
  //constructor paramerer: String
  public Roman1(String roman) // string to int?
  {
    //Calls setYearRoman() method passing the roman year parameter as the argument
    setYearRoman(roman);
  }
  //methods start
  // changes year to roman numerals
  public void setYearDecimal(int year)
  {
    //String romanNumYear = "";
    String newRoman = "";
    // Checks to make sure the year is not above 4000 or negative
    if (year > 4000)     
    {
      year = 0;
      romanNumYear = "";
    }
    else if (year < 0 )
    {
      year = 0;
      romanNumYear = "";
    }
    else
    {
      int count = 0;
      char curChar;
      int length = Integer.toString(year).length();
      String yearS = Integer.toString(year);
      
      /*finds number of zeros and subracts that from the length 
       * so that the next for loop does not loop an unneccessary number of times
       */
      for (int x = 0; x < length; x++)
      {
        curChar = yearS.charAt(x);
        if (curChar == '0')
        {
          count++;
        }
      }
      length = length - count;
      int origYear = 0;
      
      //changes year to Roman numeral
      for (int i = 0; i < length; i++)
      {
        //thousands to Roman
        if ( 0 < (year/1000) && (year/1000) < 4)
        {
          int decToRoman = year/1000;
          switch (decToRoman)
          {
            case 1: newRoman = "M";   break;
            case 2: newRoman = "MM";  break;
            case 3: newRoman = "MMM"; break; 
          }
          romanNumYear = romanNumYear + newRoman;
          year = year%1000;
          origYear = origYear + (decToRoman * 1000);
        }
        // hundreds to Roman
        else if( 0 < (year/100) && (year/100) < 10)
        {
          int decToRoman = year/100;
          switch (decToRoman)
          {
            case 1: newRoman = "C";    break;
            case 2: newRoman = "CC";   break;
            case 3: newRoman = "CCC";  break;
            case 4: newRoman = "CD";   break;
            case 5: newRoman = "D";    break;
            case 6: newRoman = "DC";   break;
            case 7: newRoman = "DCC";  break;
            case 8: newRoman = "DCCC"; break;
            case 9: newRoman = "CM";   break;
          }
          romanNumYear = romanNumYear + newRoman;
          year = year%100;
          origYear = origYear + (decToRoman * 100);
        }
        // tens to romans
        else if( 0 < (year/10) && (year/10) < 10)
        {
          int decToRoman = year/10;
          switch (decToRoman)
          {
            case 1: newRoman = "X";    break;
            case 2: newRoman = "XX";   break;
            case 3: newRoman = "XXX";  break;
            case 4: newRoman = "XL";   break;
            case 5: newRoman = "L";    break;
            case 6: newRoman = "LX";   break;
            case 7: newRoman = "LXX";  break;
            case 8: newRoman = "LXXX"; break;
            case 9: newRoman = "XC";   break;
          }
          romanNumYear = romanNumYear + newRoman;
          year = year%10;
          origYear = origYear + (decToRoman * 10); 
        }
        // ones to roman
        else if ( 0 < year && year < 10)
        {
          int decToRoman = year;
          switch (decToRoman)
          {
            case 1: newRoman = "I";    break;
            case 2: newRoman = "II";   break;
            case 3: newRoman = "III";  break;
            case 4: newRoman = "IV";   break;
            case 5: newRoman = "V";    break;
            case 6: newRoman = "VI";   break;
            case 7: newRoman = "VII";  break;
            case 8: newRoman = "VIII"; break;
            case 9: newRoman = "IX";   break;
          }
          romanNumYear = romanNumYear + newRoman;  
          origYear = origYear + decToRoman;
        }  
        romanNumYear = romanNumYear;
        decYear = origYear;
        //System.out.println(romanNumYear);
      }
    }
  }
  //changes roman numerals to year
  public void setYearRoman(String roman)
  {
    int decimal = 0;
    String romanNumeral = roman.toUpperCase();
    
    for(int x = 0;x < romanNumeral.length(); x++)
    {
      char convertToDecimal = roman.charAt(x);
      switch (convertToDecimal)
      {
        case 'M': decimal += 1000;   break;      
        case 'D': decimal += 500;    break; 
        case 'C': decimal += 100;    break;
        case 'L': decimal += 50;     break;
        case 'X': decimal += 10;     break;
        case 'V': decimal += 5;      break;
        case 'I': decimal += 1;      break;
      }
      
    }
    // Now adapt for specials
    if (romanNumeral.contains("IV"))
    {
      decimal-=2;
    }
    if (romanNumeral.contains("IX"))
    {
      decimal-=2;
    }
    if (romanNumeral.contains("XL"))
    {
      decimal-=20;
    }
    if (romanNumeral.contains("XC"))
    {
      decimal-=20;
    }
    if (romanNumeral.contains("CD"))
    {
      decimal-=200;
    }
    if (romanNumeral.contains("CM"))
    {
      decimal-=200;
    }
    if (decimal >= 4000)
    {
      // Set 'global' variables for decimal year and roman numeral String
      decimal = 0;
      roman = "";
    }
    decYear = decimal;
  }
  //gets the year
  public int getYearDecimal()
  {
    return decYear;
  }
  //gets the final roman numeral
  public String getYearRoman()
  {
    System.out.println(" get method " + romanNumYear);
    return romanNumYear;
  }
  // formulates and returns printable string
  public String toString()
  {
    String decAndRoman = "Decimal:  " + decYear + " as Roman Numerals is " + romanNumYear;
    return decAndRoman;
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: help using JUnit testing

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9580
  • View blog
  • Posts: 36,313
  • Joined: 12-June 08

Re: help using JUnit testing

Posted 13 November 2012 - 02:49 PM

I am not sure why this is in the 'Site Questions & Support'... moving to Java.
Was This Post Helpful? 0
  • +
  • -

#3 sturgis23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 12

Re: help using JUnit testing

Posted 13 November 2012 - 02:53 PM

sorry, i think i forgot to select a topic :/
Was This Post Helpful? 0
  • +
  • -

#4 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: help using JUnit testing

Posted 13 November 2012 - 02:58 PM

public void test1() {
Roman1 roman = new Roman1();
String actual = roman.setYearDecimal(1975);
assertEquals("Expected Result", expected, roman);



In line 4 (23 in the original post) you have the string "Expected value". The actual value that you expect to get goes in this place, the second value you have expected, this should contain the value that you want to test.

for example:

I call some method x that returns a String that I expect to be "X method"
public String x()
{
return "X method";
}


when I call this method from somewhere else i would use assertEquals like so:
String testString = x();
assertEquals("X method",testString);


the assertEquals statment above says check the string testString and see if it is equal to "X method".

When the program runs it will throw an exception if the values do not match. You can also include a String message that will display when the exception is thrown and it will be displayed. Using the prev example.

String testString = x();
assertEquals("Method x is broken","X method",testString);



This will now display the message "Method x is broken" when the test fails.

This post has been edited by ChillyWilly: 13 November 2012 - 03:09 PM

Was This Post Helpful? 1
  • +
  • -

#5 sturgis23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 12

Re: help using JUnit testing

Posted 13 November 2012 - 03:19 PM

I think i understand what you are saying...this still doesnt work, but did u mean something like this...
    Roman1 roman = new Roman1();
    //String expected =  "MCMDCCV";
    String actual = setYearDecimal(1975);
    assertEquals("MCMDCCV", actual); 


Was This Post Helpful? 0
  • +
  • -

#6 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: help using JUnit testing

Posted 13 November 2012 - 03:36 PM

What errors are you getting?

I read your initial post too fast. What you had looks fine, the code you commented out is ok. sorry for the confusion.

This post has been edited by ChillyWilly: 13 November 2012 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 sturgis23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 12

Re: help using JUnit testing

Posted 13 November 2012 - 03:41 PM

I got it thanks for the help, very much appreciated :D

i was calling the wrong method, needed to call the getRomanNumeral method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1