Hi everyone, this post is to let you know how can you use JUnit to test your java code inside the eclipse editor. I am going to assume that you have already installed eclipse and can execute correctly a hello world program.
- Create a new java project (which from now on I am going to call MyProject)
- Right click on the java project and go to properties.
- Inside the Properties for MyProject window go to Java Build Path >> Libraries >> select ModulePath >> Add Library and select JUnit.
- By adding this, you are now going to be able to import the JUnit package inside your project.
- Now we need to create a JUnit test case, so, in the package explorer, right click on MyProject >> new >> JUnit Test Case and a New JUnit Test Case window will pop up over there you must select which JUnit version you want to use, the source folder, the package name and so on… Click on finish whenever you want.
Coding Example:
As mentioned in the previous blog, I am going to begin with the test cases.
We are expecting to have a method that properly checks if a string that represents a new username is syntactically right according to the following rules:
- The username must be within 5 – 20 character length
- The username can not contain numeric characters
- All the characters must be lowercase
By having those rules, we can now start coding the expected behavior with some test cases.
To avoid writing an infinite amount of tests to see that our code is behaving properly we might want to select the test cases that will help us test each of the rules, this will grant us that each of those rules are being obeyed and if not, will help us see in which one it is failing.
So, let’s divide the problem:
- The username must be within 5 – 20 character length
-∞ —REJECT— 5 —–ACCEPT——- 20 ——————-REJECT—————— ∞
1.- username with length of 20 must be accepted:
username = «abcdefghijklmnopqrst»;
assertEquals(true, test.validateUsername(username));
2.- username with length of 5 must be accepted:
username = «abcde»;
assertEquals(true, test.validateUsername(username));
3.- username with length of 0 must be rejected
username = «»;
assertEquals(false, test.validateUsername(username));
4.- username with length of 25 must be rejected
username = «abcdefghijklmnopqrstuvwxy»;
assertEquals(false, test.validateUsername(username));
- The username can not contain numeric characters
5.- username with one number must be rejected
username = «abcdefghi1»;
assertEquals(false, test.validateUsername(username));
- All the characters must be lowercase
6.- username with uppercase letters must be rejected
username = «1bcA»;
assertEquals(false, test.validateUsername(username));
And now, after writing our test cases, we can write our method considering all those cases:
public boolean validateUsername(String username) {
if (username.length() < 5 || username.length() > 20) {
return false;
}
for(int i = 0; i < username.length();i++) {
if(Character.isDigit(username.charAt(i))) {
return false;
}
if(Character.isUpperCase(username.charAt(i))) {
return false;
}
}
return true;
}
Finally, we run the test cases and see if one of them fails
https://github.com/OscarSierra24/Software-testing-junit-homework