• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How can I test method that gets input and create method

 
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I test method that gets input and create method, I've got method that takes input from keyboard and create object.

I thought about assertNotNull, but don't know how to use it.
 
Rancher
Posts: 5114
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I test method that gets input and create method,


You can automate the providing of input by using the Scanner class's constructor that takes a String as argument.  Build a String with all the required values that the method will read.
You can validate the contents of the created object by adding a toString method to the class that returns a String with the values of its fields and then processing that value.
 
must Janik
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

How can I test method that gets input and create method,


You can automate the providing of input by using the Scanner class's constructor that takes a String as argument.  Build a String with all the required values that the method will read.
You can validate the contents of the created object by adding a toString method to the class that returns a String with the values of its fields and then processing that value.



I don't understand. Can you give me some example?
 
Norm Radder
Rancher
Posts: 5114
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

give me some example


Define a Scanner to provide input:
 
must Janik
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

give me some example




There is mistake in name topic, it should be: ....and create object.
Nevermind.


Define a Scanner to provide input:
How can it help me with testing?
 
Marshal
Posts: 80644
472
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please start by explaining what sort of input you require. It is very easy to use the methods of a Scanner to verify that its next token will match an int for example, which will return false before line 2 and true before line 3. It is much harder to verify that the next token has a particular text format; hasNext() only shows that you have not yet reached the end of the input. Which means, by the way, that a Scanner reading from System.in should always return true from hasNext().
You can try reading the next token and using its matches/match() method with a regular expression (=regex), or something similar, but that depends on knowing what your specification for the required input is. Beware: regexes need special care because they can be difficult to set up correctly.

Don't create multiple Scanners to read from System.in. Use a utility class that does all your keyboard input; you can set up the methods with a loop, like that shown here which will reject input in the incorrect format. As I said above, that would require special upgrading for next() to return something matching a particular regex.
 
Norm Radder
Rancher
Posts: 5114
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can it help me with testing?


It would provide all the needed input for a call to the method so that the tester does not need to enter any input manually.
 
must Janik
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah, really don't understand what is wrong with my method and why you are talking about Scanner's. Don't know what I should change in this.
 
Norm Radder
Rancher
Posts: 5114
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is wrong with my method


Why do you think there is something wrong with the method?

what I should change in this.

Nothing need be changed unless testing finds a problem.

What do you mean by "test method"?
I assume that means to pass some data to the method and check the output from the method that it is what is expected.
If you mean something else, please explain.

My suggestion was a way to pass data to the method and a way to look at the contents of the object that the method built.
 
must Janik
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

what is wrong with my method


Why do you think there is something wrong with the method?

what I should change in this.

Nothing need be changed unless testing finds a problem.

What do you mean by "test method"?
I assume that means to pass some data to the method and check the output from the method that it is what is expected.
If you mean something else, please explain.

My suggestion was a way to pass data to the method and a way to look at the contents of the object that the method built.



Ohh... I thought about unit tests.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic