0

Chris has written a function called toBinary that has an input parameter of an integer number and returns a string that represents the binary equivalent. For example, if the function is called with the integer number 3 then the returned string should be '11'.

Write a sequence of test specifications in English possibly using the "given", "when", and "then" sequence and their equivalent JUnit code.

My answer is:

The test should cover normal cases, extreme cases, and erroneous cases. Given an integer for example 3, it should then covert it to 11 after the method is executed.

@Test
public void testToBinary() {
Binary aBinary = new Binary();
assertEquals(3, 11);
assertEquals(2, 10);
assertFail(10, 8575);
}

is this correct?

2
  • when you say that toBinary "has an input parameter of an integer", do you mean it takes an int or an Integer? There is no integer that I'm aware of. If it takes an Integer, is there well-defined behavior for the null case? If so, does it throw an NPE? If so, can you assert that an NPE is thrown? Commented May 16, 2011 at 12:44
  • I would also add cases for Integer.MAX_VALUE / Integer.MIN_VALUE Commented May 16, 2011 at 12:45

4 Answers 4

6

Those asserts don't make sense -- of course, 3 != 11. You'd need apply the conversion method to the input and verify that the output is expected:

assertEquals("11", aBinary.someConvertMethod(3));

The expected value must be the first parameter, and the actual value is the second parameter.

Also, assertFail isn't a real method. There is assertNotEquals, that's probably what you're looking for.

Sign up to request clarification or add additional context in comments.

Comments

5

In your code sample, when you write 'assertEquals(3, 11);' You assert that 3 equals 11 (base ten) which will always be false.

Would you not require something like

assertEquals("11", toBinary(3));

Which tests that your function, given an input of three, returns the String "11".

Comments

1

you can add testcases for zero and negative numbers..

Comments

0

First and foremost each unit test should have at most one assert. As others have stated you must invoke the constructor or whichever method you are relying on in the actual test case:
assertEquals("11",toBinary(3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.