Solved

writing junit for my java application

Posted on 2014-09-17
8
693 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 36

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 36

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

617 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