I'm new to junit. I want to create test cases for if condition,loops. do we have any guidelines to write test cases for if conditions.Can any1 explain with an example.

Thanks in advance.


Recommended Answers

All 2 Replies

I'm not sure what you really want? Please give us some cases/examples that you want to test.

Sometimes, you could use try/catch in the test. When certain values should not be there but it all the sudden gets to there, you could throw an exception. Then catch the exception in your catch clause to confirm that it correctly throws the exception.

I'm no JUnit expert, but AFAIK it runs tests at the method level and doesn't have any facilities for testing individual loops etc inside a method. In other words, it treats the method being tested as a "black box" - it supplies data, runs the method, and checks that the results are correct. If all the results are correct then you generally assume that the code inside the method must be correct.
Maybe a JUnit expert here can add to that???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.