Defining jUnit Test cases Correctly?

As explained by the other answerers, it depends on the interface contract of your multiplier. You should think through how you (or its clients, in general) are supposed to use it, what should happen in case of specific errors or extreme cases, etc. In Java, the convention is to throw an exception in such cases.

As explained by the other answerers, it depends on the interface contract of your multiplier. You should think through how you (or its clients, in general) are supposed to use it, what should happen in case of specific errors or extreme cases, etc. In Java, the convention is to throw an exception in such cases. 2) What additional test cases did I miss?

A couple of cases which come to my mind: // test commutativity assertEquals("0", multiplier. Multiply("0", "5")); assertEquals("-1", multiplier. Multiply("1", "-1")); assertEquals("149645", multiplier.

Multiply("173", "865")); assertEquals("149645", multiplier. Multiply("865", "173")); // test some more unusual cases of multiplying with 0 assertEquals("0", multiplier. Multiply("-5", "0")); assertEquals("0", multiplier.

Multiply("0", "-0")); // test with numbers starting with '+' assertEquals("368", multiplier. Multiply("+23", "+16")); assertEquals("-368", multiplier. Multiply("-23", "+16")); // test multiplying huge values without overflow assertEquals("18446744073709551616", multiplier.

Multiply("4294967296", "4294967296")); assertEquals("18446744073709551616", multiplier. Multiply("-4294967296", "-4294967296")); 3) Is assertEquals() method enough for testing the multiplier method or do I need other methods like assertTrue(), assertFalse(), assertSame() etc In this case all you need is to compare two values for equality. In other tests you may need different kinds of asserts.4) Is this the RIGHT way to go about developing test cases?

How am I "exactly" benefiting from this exercise? There is no single "right" way to unit testing. What comes closest is probably test driven development, which is recommended by many (including myself) if you write your code from scratch.

Your benefit from this exercise is probably that you got familiar with JUnit and tried the "tester hat" on for a while. 5)What should be the ideal way to test the multiplier method? How is this question different from the previous one?

Thank you for the pointers. It makes sense now. – Epitaph Apr 25 '10 at 2:04 Can I have one method containing multiple assertEquals() for similar test cases?

For example, having 1 method testArgumentMultiply() for test cases involving illegal inputs like characters, special symbol, strings, empty string. Since, all of these inputs will result in the same NumberFormatException? – Epitaph Apr 25 '10 at 23:46 @Epitaph, purists insist that one should test only one single thing in any test method.

I am not that picky. I strive to test a single coherent use case in a test method, including possibly several asserts. In your specific case, however, note that when a method called throws an exception, the execution of the calling test method is terminated there, so no further calls will be executed.In other words, you must put each of your exception tests into its own separate test method.

– Péter Török Apr 26 '10 at 7:54.

Firstly, your code is in error because you have a class but no functions. I'm assuming all these tests are in one function? If so, I would advise against it.

Generally you want one test to test one thing so: public class MultiplierTests { @Test public void testSimpleMultiple() { assertEquals(...); } ... } Secondly, you're passing an int as the result here: assertEquals("Result", 5, multiplier. Multiply("5", "1")); but Multiplier.multiply() returns a String? So how you test this depends on what the result is.

If passed in an empty string does it throw an exception? If so you can define your @Test annotation to say it expects an exception to be thrown: @Test(expected = IllegalArgumentException. Class) public void test() { multiplier.

Multiply("", "5"); }.

In your IllegalArgumentException example, what should go in the field of expected value, if I am using the assertEquals() method with 3 parameters as below @Test(expected=NumberFormatException. Class) public void testMultiply() { assertEquals("4 * a",? , tester.

Multiply("3", "a"); } – Epitaph Apr 25 '10 at 22:45 @Epitaph in the IllegalArgumentException example, don't use assertEquals(). Just call the method. If no exception is thrown the test will fail.

If an exception other than IllegalArgumentException is thrown, the test will fail. It will only pass if IllegalArgumentException is thrown. – cletus Apr 25 '10 at 23:05 Thanks.

I tried putting in "new NumberFormatException()" for the expected value, and it worked. Is that fine too? – Epitaph Apr 25 '10 at 23:29 @Epitaph you use the appropriate exception.

I just used IllegalArgumentException as an example in my post because I didn't know what exception would be thrown or if one would be thrown at all. – cletus Apr 25 '10 at 23:35 @cletus No, I wasn't talking about the different Exceptions, but about using either just the method or the assert assertEquals() with Exception object as the value for expected parameter. It seems both are fine.

Again, thanks for the input. – Epitaph Apr 25 '10 at 23:39.

You should consider adding test cases that verify your exceptions are working fine using the @Expected annotation. Basically, write a case that you know should generate a exception and then see that the test passes. A good way to determine if you have missed any cases in complex methods is to run them through a code coverage tool.

Run all your tests and then look at the code coverage result. If there are parts of your code that are not visited by your test cases, you are probably missing some. You can find a good guide here.

– Carl Apr 24 '10 at 4:21 There is a good list here: java-source. Net/open-source/code-coverage. I suggest you try codecover.

Org I personally use Clover from Atlassian but that is not a free tool. – Prachi Apr 24 '10 at 17:32.

If you are really testing edge conditions, and you are expecting string representation of numbers, perhaps you can test passing strings to the function method under test @Test(expected= NumberFormatException. Class) public void test() { multiplier. Multiply("a", "b"); } I disagree with cletus on his implementation, as his test case expects an all encompassing IllegalArgumentException, I think it is better to test for specific sub classes than using a parent exception.

I believe the IllegalArgumentException was supposed to be an example only. – Péter Török Apr 24 '10 at 22:36 I concede that point but fact is expecting a parent exception can make you catch lot of other unexpected exceptions. – Kartik Apr 26 '10 at 18:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions