Solved

writing junit for my java application

Posted on 2014-09-17
8
683 Views
Last Modified: 2014-09-24
public class Pyramid {
	public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramid pyramid = new Pyramid();
		pyramid.display(ar);
	}

	public void display(int lineNumberCount) {


	     for( i=0;i<5;i++) {
	         for(j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }

	
	}
}

Open in new window


I have written java application like above to print the pyramid. I would like to know how to write junit for this program. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 40329373
JUnit is about testing code. It allows execution of "code under test" with various inputs and it can then "assert" that the output received from that code is what is expected.

The code that you currently have doesn't not have any (useful) inputs, ie. it does the same thing each and every time it is executed. Also, it doesn't have any output that is useful for JUnit to check (console output is NOT something that JUnit can get and so it can't assert that the correct information was printed on the console)

What were YOU expecting JUnit to do for you?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 40329826
First of all,
the code you posted above, did you already ran it?
I don't think so, because
1) it doesn't even compile. Since the type of the variables i, j  and k is not defined.
2) the parameter lineNumberCount in your method display() is never used!


JUnit is intended to test each and every method is doing what is expected from it.
Eg. in your Rectangle class you have a method getPerimeter() that calculates the perimeter given the lenght and width of the Rectangle:

public int getPerimeter() {
   return 2 * getWidth() + 2 * getLength();
}

With JUnit you could test the different cases: for "normal" widhts and lengths, for a length of 0, for a width of zero, and for both being zero.

Your Pyramid class just has a display() method. Not much to unit test.

You could split it's functionality up as follows:

public class Pyramid {
    public static void main(String[] args) {
        int ar = Integer.parseInt(args[0]);
        Pyramid pyramid = new Pyramid();
        pyramid.display(ar);
    }

    private void drawLine(int lineNumberCount, int currentLine) {
        for(int j=0;j<lineNumberCount-currentLine;j++) {
            System.out.print(" ");
        }
        for(int k=0;k<=currentLine;k++) {
            System.out.print("$ ");
        }
        System.out.println();
    }

    public void display(int lineNumberCount) {
        for( int i=0;i<lineNumberCount;i++) {
            drawLine(lineNumberCount, i);
        }
    }
}

Open in new window


Then, using a mocking framework like mockito, you could test that if you call drawLine() with the parameters X and Y,
 System.out.print() is called Q times with parameter "$ " and P times with parameter " ".
0
 
LVL 7

Author Comment

by:gudii9
ID: 40330966
public class Pyramid {
	public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramid pyramid = new Pyramid();
		pyramid.display(ar);
	}

	public void display(int lineNumberCount) {


	     for( i=0;i<ar;i++) {
	         for(j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }

	
	}
}

Open in new window


I corrected small type and above code is the correct one where i am passing instead of five ar now.  I am even fine to pass ar myself rather than eclipse run configuration if that would me writing junit easy. please advise
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 35

Expert Comment

by:mccarl
ID: 40331738
Can you please answer the question that I posted above...
What were YOU expecting JUnit to do for you?

(Also, that code still won't even compile)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40332107
I would appreciate that the code you post, would be code that you really compiled and run for yourself.

I repeat:
>> 1) it doesn't even compile. Since the type of the variables i, j  and k is not defined.
>> 2) the parameter lineNumberCount in your method display() is never used!

Look at the code I provided and see what you can learn from that.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40332575
package com.ti.sample;
public class Pyramids {
	public static void main(String[] args) {
		int ar = Integer.parseInt(args[0]);
		Pyramids pyramids = new Pyramids();
		pyramids.display(ar);
	}

	public void display(int lineNumberCount) {


	     for(int i=0;i<lineNumberCount;i++) {
	         for(int j=0;j<5-i;j++) {
	             System.out.print(" ");
	         }
	        for(int k=0;k<=i;k++) {
	            System.out.print("$ ");
	        }
	        System.out.println();  
	    }

	
	}
}

Open in new window


Sorry for the typos. I corrected tested by passing 6 in the run configuration and got out put as below
     $
    $ $
   $ $ $
  $ $ $ $
 $ $ $ $ $
$ $ $ $ $ $


I expect JUNit to do the unit test to see whether same number of line Count being passed is displayed in the output pyramid row. In this case i am pasing 6 as arument and i see 6 rows are coming in output console. I want that to be validated by junit
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40336104
If I run the last code you posted with 10 as parameter I got:

     $
    $ $
   $ $ $
  $ $ $ $
 $ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $ $ $
$ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $

This reveals that:
1) There's still an error in your code. I'll leave it to you to find it. (hint: as I said before, have a look at the code I posted)
2) Checking that 10 rows are printed if you pass 10 as parameter is not enough.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40337201
sure. I will check.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Eclipse Loop 3 48
Tomcat 9 + java 8 error while trying to deploy a war file 2 853
Java Inheritance super keyword use 8 72
Session in java desktop 5 37
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question