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.
Related
My program produces a list of Strings in one activity, then passes it to another activity, and the second activity uses the strings.
When I test by printing out each element of the list at the beginning of the second activity, the printout looks perfect. For example, if I am hoping for the list to contain "Lemon Juice", it prints out exactly right, but yet the logic in the second activity still doesn't work. If I add "Lemon Juice" just like that to the list manually, the logic in the second activity works fine, so the issue is that somehow the string in the received List is not really "Lemon Juice". But:
It prints out exactly correctly (including checking for spaces in front and at the end).
I have tried explicitly casting the received list elements as (String) just to be sure they are Strings.
If I run "Lemon Juice".contains(received String) it comes back true, and if I run received String.contains("Lemon Juice") it
comes back true, but if I run received String.equals("Lemon Juice") it comes back false. This is very confusing to me.
Can anyone think of a possible explanation for how something that is cast as a string, prints as a string, and looks like a string, is not performing like a string?
EDIT to include some code as requested:
// instance variable at top of class--list to which strings will be added for use in
// 2nd activity
private List<String> exs = new ArrayList<String>();
// get array of strings from extra from intent from first activity
String[] recExs = getIntent().getStringArrayExtra(BrowseActivity.EXS);
for (int exx = 0; exx < recExs.length; exx++) {
String curEx = (String) recExs[exx];
exs.add(curEx);
}
Somehow, when I pass exs to the method I need to use the strings, it doesn't work, even though, as explained above, printing and calling contains etc all show that the string, before I added it to exs, was at I wanted it to be.
It's hard to help when you only posted a small snippet of your code.
But I'm guessing the reason String.contains works but String.equals doesn't is that maybe there is space in the Strings. Try String.trim on both side of the Activity when passing and receiving data.
Some tests are used with this annotation #RunWith(Theories.class) in JUnit and I don't know when and why we use it?
You should use them when you would like your tests to focus on the generalized relationship between inputs and outputs. See: https://blogs.oracle.com/jacobc/entry/junit_theories.
I think capturing multiple types of input are one thing. The high level idea is that you want to test if your method is true for all possible inputs.
For example, let's say I have some complex business process that takes 5 different inputs, and let's say for each input, there's 10 possible states, so we end up with 10*10*10*10*10 = 100,000 possible input states, which means we need to know beforehand what all these 100,000 output values are.
However, you probably realize that you don't need to actually enumerate all 100,000 states. There's probably a subset that you are interested in. Let's theorize for example:
"Admins have no permission restriction". And if I wanted to assert that this is true, my Test ends up looking like the pseudo-code below.
#Test
public void AdminsHaveNoPermissionRestriction(User user, BusinessProcess bp, Input a, Input b ...) {
Assume.assumeThat("User is an admin", user.hasRole(admin);
// .. rest of test which uses bp, a, b etc...
)
The nice thing is that we skip un-interested objects (non-admins) because it fails the assumptions.
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.
Hi I am having a problem with my while loop in the getAmatch() function
It doesn't enter the while loop in android but enters in a normal Java class.
All I can say off the top of my head without a bit more information is watch your case sensitivity on both input and patterns, and your while loop is a good piece of evidence; your matcher.find() is simply not finding a match. As it states in the documentation:
Parameters:
start: The index in the input at which
the find operation is to begin. If
this is less than the start of the
region, it is automatically adjusted
to that value. If it is beyond the end
of the region, the method will fail.
Returns:
true if (and only if)** a match has been found.
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.