Solved

writing junit for my java application

Posted on 2014-09-17
8
651 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-position sub-options beneath the TAB 7 74
stackato and cloud 4 68
servlet doXXX methods 3 35
tomcat not starting 6 26
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

18 Experts available now in Live!

Get 1:1 Help Now