Solved

writing junit for my java application

Posted on 2014-09-17
8
648 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now