I'm being asked to sketch a Junit test case for a method:
public int checkOutItems(ArrayList<int> trolley)
which will return an ArrayList of item name and price list.
It is also defined the types of test case like normal condition,boundary, and exceptional.
I'm just wondering what is meant by boundary and exceptional?
Normal will just be to compare the input and output but boundary and exceptional?
Boundary tests
Let's say you need to test something that adds up to 50 numbers together. Your first boundary test would be to pass a list containing no items, the lower boundary. Your next test would be to pass a list that contains 50 numbers, the upper boundary. Boundary tests are often easy to write as you try to test the maximum or minimum (or a combination of them) parameter values. These should all work, of course, otherwise they belong in the next section.
Exception tests
What will happen if you pass a null list to a method? What happens if your list contains 51 items instead of the 50 maximum? What about passing in a list of Strings instead of Integers, for example? Exception tests are designed to try and break the program, either to test known limitations, or to test exception handling.
Boundary test - pass boundary valid arguments (like pass empty array to length function and check that result is zero)
Exceptional test - pass invalid argument and check that appropriate exception is raised.
Related
I have a loop in my code which goes over a set of strings. Said strings are then passed along to several other functions.
In my tests, I'm basically emulating the flow of code and asserting at the start of each method I expect it to visit, if the string is correct.
But I can only write the test method once. Meaning that I have to do the following to catch all the different strings:
assertTrue(string.equals("test1") || string.equals("test2") || string.equals("test3") || ...);
However, there's a problem with this if one or more of those strings are not successfully passed to the list that is looped over. Since this is a chain of OR statements, it will succeed as long as there is 1 correct string, regardless of whether any of the other strings are missing. Which is a problem.
I can't emulate the loop, I can only emulate the functions receiving the data each time.
Is there a way to deal with this problem?
EDIT: some clarification.
I start out with a list of strings.
This list gets looped over, meaning every single string instance will go through a bunch of functions. And then the next string. And so on.
In the test, I can write dummies for the methods the string goes through. This means I override the behavior of the actual code and send my own custom return. This has to be correct though, since the function following that has to properly process what I just send to it.
But, when I start the test with the dummy data, it will do the loop, meaning the same function gets called multiple times, each time with a different string. I can't just do 1 test for one of the strings, because the next loop will fail on the next string.
I am not looking for the answer to this question but just a brief outline of how to do it. This is a question from a exam past paper.
It states: Describe in English a sequence of tests that you might use to test code to implement the NumberCoversion class. Your tests should cover all the conditions described in the above definition.
I won't write the specification of the class but it contains things like: it must take String as input and output, accepting two parameters and returning null if a number is not valid etc.
The question is worth 10% so will I just be required to write a series of things like: Ensure that the constructor only accepts two parameters of type int, and not anything else e.g. double or accept 3 parameters.
Would it be worth writing possible JUnit test methods in English/Pseudocode.
Would this be the right sort of thing to write for tests in English?
I think the goal is to describe a test case which checks each of the specifications in the question, whilst also avoiding attempting to test things which are limited by the language construct (e.g. wrong number/type of arguments).
Describe in English what you will do if you write tests. Typically it's usage of the NumberConversion class.
According to the question, you need to describe tests in English. I think that jumping to JUnit unit tests is more than being asked for. If I answered this question, I would start by looking at the definition given of the NumberConversion class. I would describe tests that use valid inputs as well as tests that use invalid ones. For each test describe how you will ensure that the NumberConversion class behaves as expected, including expected error conditions.
As an example for what might be appropriate...
If the specifications were:
Takes a string as input
The string can be an arbitrarily large non-negative integer
If the string is not a non-negative integer, an exception is thrown
Then I would probably answer something along the lines of:
I would test with "42" as input to check that the method works with a "normal" number
I would test with "0" as input to check that the method works with the edge case number
I would test with "9223372036854775808" (1 more than Long.MAX_VALUE) as input to check that the method works with a number larger than the fixed length integers provided by Java
I would test with "-1" as input and ensure that an exception is thrown, as negative input is invalid
I would test witht "0xa" as input and ensure that an exception is thrown, as hexadecimal input is invalid
I would test witht "0.1" as input and ensure that an exception is thrown, as non integral input is invalid
I have a method that searches for an element using binary search. I have created two test methods to test it. The first one asserts that the index returned by the method is the same as the index I give during the test. And the second test method asserts that the method returns -1 when the element is not in the array.
Are those two methods sufficient?
I am using Java
How about these cases.
The element occurs twice in the array.
If the array stores objects (rather than primitives), then consider the case when the array has
an element that is equal to the object that your searching for, but
is not the same object.
Searching for null.
When the array has no elements.
Searching for an element that's greater than everything in the array.
Searching for an element that's less than everything in the array.
Your test results should coincide with the business and functional requirements of your application. It sounds like you've covered the "happy path" scenario. Now you'll want to focus on edge cases which may include something like having empty strings to search against or when searching for a "-1".
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?
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.
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".
you can add testcases for zero and negative numbers..
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);
I have a question which is described below:
What problems would arise for testing a Java class which counts number of words in a file?
The function's signature is below:
public int wordCount(String filename)
Well, this is a junit testing question.
If you know the problem, what is the solution of that?
So your question is what to test for? If yes, I'd say you should check if the definition of "word" is implemented correctly (e.g. is "stack-overflow" one word or two), are new lines handled correctly, are numbers counted as words (e.g. difference between "8" and "eight"), are (groups of special) characters (e.g. a hyphen) counted correctly.
Additionally, you should test whether the method returns the expected value (or exception) if the file does not exist.
This should be a good starting point.
To sfussenegger's list, I'd add the file handling checks: does the method respond correctly to files not found (including null filename), or lacking read permission?
Also, to sfussenegger's correctness list, I'd add whether duplicates count and case sensitivity rules, as well.
Of course, all of this requires that you know how the method is supposed to behave for all of these specifics. It's easy to tell someone to "go count words", but there are subtleties to that assignment.
Which is one of the big benefits of writing a good set of unit tests.
This really sounds like a task for FIT: Framework for Integrated Test. It's an acceptance testing framework that works with ant and JUnit.
One docent of mine did such a task and used this framework. It allows you to write a whole bunch of test cases within one html/wiki table. FIT will interpret each line as a parameter set for the function under test and checks the output.
For example:
This table displays the result of three test cases. Two passed, one failed.
You can use fit if you write sentences and define the number of words in your table. With FIT, they're executed and the result is displayed in a new table.
For further information, please read Introduction to FIT.