[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Junit Testing

Posted on 2005-04-26
8
Medium Priority
?
358 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:JimmyHackNSlash
8 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13865117
try instanceof
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13865137
>> 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
 

Author Comment

by:JimmyHackNSlash
ID: 13865186
But it keeps failing the junit tests. I will try instance of.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 21

Expert Comment

by:MogalManic
ID: 13865320
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
 

Author Comment

by:JimmyHackNSlash
ID: 13865442
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
 

Author Comment

by:JimmyHackNSlash
ID: 13865551
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
 
LVL 37

Accepted Solution

by:
zzynx earned 280 total points
ID: 13866020
>> Instance off wasnt available
?????? What do you mean???

Try

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

Expert Comment

by:zzynx
ID: 13867603
thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 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