This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How do I test an object boolean method.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?

 
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?




First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.

Second, once you determined the type, you can cast the object -- and at which point, you should be able to access the instance variables (and accessor methods).

Henry
 
Henry Wong
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roger Sterling wrote:Don't double-post.

http://stackoverflow.com/questions/6119392/junit-testing-for-a-boolean-method




Thanks Roger. Have a cow.


To the OP... The ranch actually allows cross-posting. We just ask that you be forthright about it.... https://coderanch.com/how-to/java/BeForthrightWhenCrossPostingToOtherSites

Henry
 
Tate Hsumsor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hnery

Henry Wong wrote:

Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?




First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.

Second, once you determined the type, you can cast the object -- and at which point, you should be able to access the instance variables (and accessor methods).

Henry

 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:First, you can use the instanceof operator to test the instance to a class type -- not need to use the Class classes.


It depends on what you want to test.
Tate's code will test if obj is an instance of the Fraction class.
Using instanceof will test if obj is an instance of the Fraction class or a subclass of Fraction.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tate Hsumsor wrote:I have been writing test scripts for my class but I'm stuck on this method. how do i test for an object?


Sounds like you're learning about writing equals() methods; and there's a LOT to know. According to this article by Martin Odersky:

"...after studying a large body of Java code, the authors of a 2007 paper concluded that almost all implementations of equals() are faulty"

Personally, I'm with Henry: use instanceof - it's more flexible, and it also saves you having to check for null (which I notice you haven't done) - but do digest Stuart's post as well.

If you're really interested, you might want to have a look at the FirstClasses page, which goes into the process in some detail (and shows you an alternate way of doing it, based on the above article) - but I warn you: it's not short.

Winston
 
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problems occur with instanceof if you have a subclass with additional fields used in equals().
You can get superObject.equals(subObject) and subObject.equals(superObject) returning true and false respectively, so you have breached the general contract of equals.

I suspect a major reason for faulty code is that teachers don't all know about the general contract and teach people to overload equals rather than overriding it.
 
Campbell Ritchie
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . have a look at the FirstClasses page, . . . Winston

The class described there is marked final, so the problem with instanceof I mentioned earlier cannot occur.

There are three good references about equals which I know of: they are listed in this post. Read all three; they don't all cover exactly the same material. I think Odersky Spoon and Venners has already been mentioned in this discussion.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The class described there is marked final, so the problem with instanceof I mentioned earlier cannot occur.


Actually, that's just to start with; it also goes into the whole business of subclassing, and what it means for equals().

Winston
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Problems occur with instanceof if you have a subclass with additional fields used in equals().


Actually, problems occur with equals(), period. Using getClass() is a draconian solution (a "non-solution", IMO) to the problem, and needs to be clearly documented; otherwise you can run into LSP issues when dealing with collections.

@Tate: And if all this discussion seems quite advanced, it is. I'm afraid that equals() in an "objective" language just ain't simple.

Winston
 
Campbell Ritchie
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: the Bloch reference describes problems with getClass() and the LSP, with classes much simpler than Collections.
 
Tate Hsumsor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much for your contributions Winston you're right my head was spinning a bit after reading all the replies, but I am looking at the links added in the responses and I am learning something new, I didn't know there were so many ways to write an equals method. So It's all very helpful...
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tate Hsumsor wrote:but I am looking at the links added in the responses and I am learning something new, I didn't know there were so many ways to write an equals method. So It's all very helpful...


Glad you're finding it useful. Even if you don't understand them all right now, it'll certainly put you ahead of the game (at least as far as equals() is concerned ).

Winston
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!