• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write a Driver class to test methods  RSS feed

 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so I have an assignment where I am to create a Array class then create a Driver class (TestArray) to test all the methods in the Array Class. I have no idea how to do this, can you guys help me please ? Here's the code i've written for the Array Class. I just need help developing the TestArray class.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you allowed to use Junit for testing? If yes, then there are lots of tutorial available online. They are easy to follow. You can check them.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you not written a constructor for your class? Write one constructor which takes an array as its parameter and forget about new double[]; which won't compile anyway because you haven't supplied a size for the array. Then you can call that constructor from the test class' methods:The reason for using the clone method is to take a defensive copy. You can read about defensive copies here, where we were discussing Effective Java™ by Joshua Bloch.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're not going to use a testing framework like JUnit, then what you have to do is just create a new class named TestArray with a public static void main(String[] args) method.

In there, you can create new instances of your Array class and call methods on it and print out the results, so that you can see if it works as you intended.
 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:If you're not going to use a testing framework like JUnit, then what you have to do is just create a new class named TestArray with a public static void main(String[] args) method.

In there, you can create new instances of your Array class and call methods on it and print out the results, so that you can see if it works as you intended.


Can you show my how I would do this with my program ? Just call one method I could use as an example ?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating a new instance of your class, then calling its methods in turn. Obviously you will have different methods in your class.
you should override the toString method so you get a decent printout from println. You can read about toString in lots of places; this sample chapter from Joshua Bloch's Effective Java™ is one of them.
 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are creating a new instance of your class, then calling its methods in turn. Obviously you will have different methods in your class.
You should override the toString method so you get a decent printout from println. You can read about toString in lots of places; this sample chapter from Joshua Bloch's Effective Java™ is one of them.


How would this work in relation to my code though ? I don't have any numbers so its throwing me off.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just for example, you might first set an array into an instance of your class. Then you might call the isInIncreasingOrder method on that instance and see if it returns the right value. The "right" value would of course depend on what values were in the array you passed to the instance. So you would try this with an array which is in increasing order, and then with one which isn't in increasing order, and see if isInIncreasingOrder() returns "true" for the first and "false" for the second.

I expect this is already clear to you, but the code you're going to write in the Array class has a purpose and the tests you write are supposed to find out whether the code actually fits that purpose.
 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, just for example, you might first set an array into an instance of your class. Then you might call the isInIncreasingOrder method on that instance and see if it returns the right value. The "right" value would of course depend on what values were in the array you passed to the instance. So you would try this with an array which is in increasing order, and then with one which isn't in increasing order, and see if isInIncreasingOrder() returns "true" for the first and "false" for the second.

I expect this is already clear to you, but the code you're going to write in the Array class has a purpose and the tests you write are supposed to find out whether the code actually fits that purpose.


lol I'm hopeless this is making no sense to me. Yes I do understand that the Test is going to verify the functionality of the Array class. But I have no idea how to write this in my program.
 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is literally all I understand

 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put it to you in plain English then.

Say you have an array, {1, 2, 4, 5, 6}. Given this array,

- the isInIncreasingOrder() method should return true
- the isInDecreasingOrder() method should return false
- the getTotal() method should return 18
- the getAverage() method should return 3.6

You'll need to figure out a way to translate this into code that tests for these conditions. We can't do it for you.

Edit: Well, we could but we won't. We'll let you do the honors and have all the fun. Of course, you'll get the well-deserved credit, too.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one example to demonstrate at least the idea.
I'm going to do it as a JUnit test, but it should at least explain the gist of it.



That explains one simple test case.
What other cases could you test? What happens if you pass an empty array? What about the array [3,2,1]

Edit: @Junilu. Just one example can't hurt can it?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regarding your comments here, you don't have to set a value for the array length. An array already has a length property that you can access. You will need to update an instance variable in your class so that it can "remember" the array that was passed in as a parameter to this method. Also, you don't want to write code in this method that asks the user to input data for the array. That activity should be done outside this method, before it is called. You will then pass the result of that, the array with all the numbers that the user entered, to the setArray() method.
 
LaMarkus Willins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Here is one example to demonstrate at least the idea.
I'm going to do it as a JUnit test, but it should at least explain the gist of it.



That explains one simple test case.
What other cases could you test? What happens if you pass an empty array? What about the array [3,2,1]

Edit: @Junilu. Just one example can't hurt can it?


Lol thanks this exactly what I needed thanks, just an example lol.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:...
I'm going to do it as a JUnit test, but it should at least explain the gist of it.
...
Edit: @Junilu. Just one example can't hurt can it?

I was just trying to stay in line with what others had given so far. It's a double-edged sword though. Giving OP the benefit of the doubt, the example could be informative. But then again, JUnit may be well above OPs head right now and could lead to even more confusion on his part.

Edit: Ok, I guess OP benefitted from the benefit you gave him
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!