Junit Testing

Hi all

Im trying to do a Junit test on some code I have. Basically I want to make sure that the return type is of the correct type, as all my method does is return a type.

eg:

public void getBooleanTest Type() {

try {
    BooleanType myBool = (BooleanType)myM.getBooleanType();

    }  
catch(Exception ex){
     fail("return type failed");
}

}
}

I know you cannot arbitrarily cast in Java, but Im trying to figure out a way to make Junit pass if both types are the same, and I thought if the cast worked then it would pass since both types are the same. Any other ideas?
JimmyHackNSlashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
bloodredsunCommented:
try instanceof
0
 
zzynxSoftware engineerCommented:
>> I thought if the cast worked then it would pass since both types are the same.
That's OK.

>> Any other ideas?
Why? The above is perfect
0
 
JimmyHackNSlashAuthor Commented:
But it keeps failing the junit tests. I will try instance of.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
MogalManicCommented:
The reason your code failed was that the ClassCastException is a Runtime exception.  Catching Exception does not trap RunTime exceptions.  Changing the catch clause to "catch(RuntimeException ex)" would work, but it is more clear if you catch just ClassCastException.  This makes it clear to the reader of the code that you are expecting the type to be BooleanType, otherwise it is a failure.  If some OTHER exception happens (i.e. NullPointerException) it will still fail, but it will fail because of the exception (different category in the report).
0
 
JimmyHackNSlashAuthor Commented:
Ok I have tested all the other Junit test cases, and the only ones failing are the casting ones. I tried to use ClassCastException and that didnt work. Instance off wasnt available.

Are you sure I can cast in this way?
0
 
JimmyHackNSlashAuthor Commented:
I have tested the casting with other types (Strings, ints etc) the problem only seems to be there, when doing BooleanType etc??

Any ideas on this
0
 
zzynxSoftware engineerCommented:
>> Instance off wasnt available
?????? What do you mean???

Try

public void getBooleanTest Type() {
    if (!myM.getBooleanType() instanceof BooleanType)
       fail("return type failed");
}
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
zzynxSoftware engineerCommented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.