Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

writing junit for my java application

Posted on 2014-09-17
8
Medium Priority
?
707 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 1000 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 1000 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

A solution for Fortify Path Manipulation.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

664 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