Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

How would I create a JUnit test for my method of multiplying fractions?

I am doing a project where I'm running a JUnit test on fractions. Now, it's only the first week of the semester, so why the professor is having us start out with such a difficult type of testing instead of something more simple? I don't know. However, I found a way to get the program to run and give me a fraction answer through the toString method, which I didn't get it to simplify fractions yet, but a start. However, I cannot for the life of me anywhere on the internet find an example of a JUnit test for this. I have to manually run the test with numbers I added myself.

Here is the code I have so far.

First.... the Fractions class with the method.....

        public class Fractions {
        int num;
        int denom;

      public Fractions (int a, int b){
     num= a;
    denom= b;
     }
       public Fractions (Fractions other){
       num= other.num;
      denom= other.denom;
          }


             public Fractions multiplyFraction(Fractions other){
           Fractions result= new Fractions(num * other.num, denom* other.denom);
         return result;

             }
            public String toString(){
        return " " + num + "/"+ denom;
           }
           }

Now, here is how I tested it from the main method, and it outputs the correct result to the screen....

          public class Testing {

        public static void main(String[] args) {
    // TODO Auto-generated method stub

    Fractions f1= new Fractions(1,2);
    Fractions f2= new Fractions(3,4);
    Fractions f3= f1.multiplyFraction(f2);

    System.out.println(f3);
}

        }

The result comes out to 3/8, which is correct. The one problem I have is the assignment instructions say the method is supposed to be a static method, in which I can't get it to work in any way shape or form with it being static, but that's another problem I'll worry about later.

However, I still cannot figure out any way to get a JUnit test that allows me to pass in 2 fractions and verify the result is what it is supposed to be. I'm so lost. The JUnit testing for adding, subtracting, multiplying, or dividing two different numbers does not work here.

I've tried declaring a variable and putting in arguments in for those variables in a assertEquals statement, but nothing I try works.

The Fractions class videos and instructions online just say what to do, which is the kind of code I did, but they don't say how to do any unit tests for it. I try to research these questions for myself before asking, but I've been searching for over 3 hours now and I cannot find anything.

Any help here will be appreciated.