patternjavaMinor
Random letter test
Viewed 0 times
randomtestletter
Problem
My first test (
Is the first test valuable enough to keep it? What about the second one? Uncle Bob say something about tests to understand how the language works. I don't remember when/where.
Do you have suggestions to improve its readability?
randomUpperCaseLetter) checks if the returned random letter is an uppercase ASCII one. The second one (compareCharactersWithAsciiCodes) is just me trying to understand Java.package com.company.letter;
import com.company.RandomLetter;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class RandomLetterTest {
@Test
public void randomUpperCaseLetter() {
char letter = new RandomLetter().generate();
int upperCaseAAsciiCode = 65;
int upperCaseZAsciiCode = 90;
assertTrue(letter >= upperCaseAAsciiCode && letter <= upperCaseZAsciiCode);
}
@Test
public void compareCharactersWithAsciiCodes() {
assertEquals(65, 'A');
assertEquals(90, 'Z');
assertEquals(76, 'L');
assertEquals(77, 'M');
assertEquals(78, 'N');
}
}Is the first test valuable enough to keep it? What about the second one? Uncle Bob say something about tests to understand how the language works. I don't remember when/where.
Do you have suggestions to improve its readability?
Solution
Without having access to the class
The name of the test looks good to me; we sure do test that an uppercase letter has been generated. The thing is we don't test if it's actually random. You're producing only one letter, checking if it's in the range of uppercase letters, and then be done with it. We've never actually checked that the letters that are produced are different/random every time. How can you be sure that it's random?
The name is still good to me, we are indeed comparing
So what can you do? First, I'm glad you have unit tests! This is one of the things I would like to see more often when someone is learning a language. Second, try to cover every bit of your code by asking (but not limited to):
There are tools to help you see what is the coverage of your tests, but you can begin with manually checking what your tests cover.
RandomLetter let's see what we can deduce from your tests. @Test
public void randomUpperCaseLetter() {
char letter = new RandomLetter().generate();
int upperCaseAAsciiCode = 65;
int upperCaseZAsciiCode = 90;
assertTrue(letter >= upperCaseAAsciiCode && letter <= upperCaseZAsciiCode);
}The name of the test looks good to me; we sure do test that an uppercase letter has been generated. The thing is we don't test if it's actually random. You're producing only one letter, checking if it's in the range of uppercase letters, and then be done with it. We've never actually checked that the letters that are produced are different/random every time. How can you be sure that it's random?
@Test
public void compareCharactersWithAsciiCodes() {
assertEquals(65, 'A');
assertEquals(90, 'Z');
assertEquals(76, 'L');
assertEquals(77, 'M');
assertEquals(78, 'N');
}The name is still good to me, we are indeed comparing
char with ASCII codes. The thing is, it's not testing anything related to RandomLetter. It's just testing if in Java the char A is equal to 65; and unless the language changes, your test is not really useful to test the class. So what can you do? First, I'm glad you have unit tests! This is one of the things I would like to see more often when someone is learning a language. Second, try to cover every bit of your code by asking (but not limited to):
- Did you test all the
ifelseof your code?
- Did you try to test that
generate()gives some different letters each time?
- Did you test all the public methods ? Did you test some corner/edge cases of your code?
There are tools to help you see what is the coverage of your tests, but you can begin with manually checking what your tests cover.
Code Snippets
@Test
public void randomUpperCaseLetter() {
char letter = new RandomLetter().generate();
int upperCaseAAsciiCode = 65;
int upperCaseZAsciiCode = 90;
assertTrue(letter >= upperCaseAAsciiCode && letter <= upperCaseZAsciiCode);
}@Test
public void compareCharactersWithAsciiCodes() {
assertEquals(65, 'A');
assertEquals(90, 'Z');
assertEquals(76, 'L');
assertEquals(77, 'M');
assertEquals(78, 'N');
}Context
StackExchange Code Review Q#90011, answer score: 4
Revisions (0)
No revisions yet.