Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using Junit testing to test

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Using Junit testing to test

    Hello, I am working on a project for my Intermediate programming class. I have the code and it works very well, but my professor wants us to use Junit testing to test our code. I've never used JUnit before, and have no idea how it works, and he provides little to no instructions/guidance on how to use it. Can someone please help me?

    And on a side note, can anyone tell me if it is possible to have a boolean value (true or false) randomly set?

    Here is the code I need to test:
    package musicalinstruments;
     
     
    class MusicalInstrument {
        public String name;
        public boolean isPlaying;
        public boolean isTuned;
     
     
        public MusicalInstrument(){
            isPlaying = false;
            isTuned = false;
    }
        public MusicalInstrument(String name){
            this.name = name;
    }
    public void setName(String value){
    name = value;
    }
    public String getName(){
    return name;
    }
    public void play(){
    isPlaying = true;
    }
     
    }
    class CWoodwind extends MusicalInstrument {
    private final String playSound;
    public CWoodwind(String name,String play){
    super(name);
    playSound = play;
    }
    public void TestTune(){
        System.out.println("" + super.getName() + " is playing out of tune");
        isTuned = true;
     
    }
    public void TuneInstrument(){
        if (isTuned == true){
            System.out.println("" + super.getName() + " has been tuned.");
            isPlaying = true;
    } else{
            System.out.println(""+ super.getName()+ " has not been tuned.");
            isPlaying = false;
        }
    }
    public void howToPlay(){
        if (isPlaying == true){
    System.out.println("" + super.getName() + " is playing using a " +playSound );
    }
        else{
            System.out.println(""+ super.getName() + " is not playing because it is not tuned.");
            isPlaying = false;
        }
    }
    public void unTune(){
        if (isPlaying == true){
        System.out.println("" + super.getName() + " is no longer in tune.");
        isTuned = false;
    } else{
     
        }
    }
    public void StopPlay(){
        if ( isPlaying == true){
        System.out.println("" + super.getName() + " has stopped playing.");
        isPlaying = false;
    }else{}
     
    }
    }
     
    public class MusicalInstruments {
    //class Main {
        public static void main(String args[]){
     
    CWoodwind wood = new CWoodwind("Woodwind","reed");
    CBrass brass = new CBrass("Brass","mouth piece");
    CString str = new CString("String","pluck and/or a bow");
    CPercussion per = new CPercussion("Percussion","hitting");
    wood.TestTune();
    wood.TuneInstrument();
    wood.howToPlay();
    wood.unTune();
    wood.StopPlay();
    }
    }
    Last edited by egreenhorn; June 15th, 2014 at 11:34 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using Junit testing to test

    have a boolean value (true or false) randomly set?
    See the Random class's API doc.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Using JUnit to test JSwing
    By Lerox in forum Java Theory & Questions
    Replies: 0
    Last Post: March 5th, 2014, 03:21 PM
  2. NullPointerException with JUnit Testing
    By 3sbwya in forum Exceptions
    Replies: 3
    Last Post: February 28th, 2014, 07:47 PM
  3. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM
  4. validate XMl by using XMLUnit and JUnit test?
    By beruska in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2011, 08:20 AM
  5. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM