Write Tests Part 2.1 – How to Write Tests in Java
hey there,
last month I wrote about UnitTests and this time I would like to show you how such tests can be written in Java and in two weeks I’ll show you how it’s done in Pharo. Isn’t this cool? Two column articles in one month! Although the new semester has started a few weeks ago I’m more into writing articles then ever.
How to Write Tests
First of all like I mentioned last month there are a few different methods for testing. The most important in Java is assertEquals as it gives more helpful output if the test fails. There are other assertion methods and if you want, have a look at the Assert-JavaDoc.
I showed you how to create test cases and the important testing methods so let’s start writing tests. Since JUnit 4 it’s much easier to write tests because you don’t have to care about testsuites and tests don’t need the word “test” in their method name to be recognized as a test. To go on with there are only a few things you have to do to write tests.
Firstly if you want to test complex classes and programs you have to initialize the required objects and their states. To do so the first method in our test case is named setUp and in front of this method you have to write @Before and import the necessary package. This setUp method will be called before each test you execute.
The next step is to write the actual test methods. You create a new test method by simply writing @Test in front of the method, isn’t it easy? Note that it’s considered good style to use method names starting with should or shouldNot and then write what you want to test so that it is clear, what is asserted. Have a look at the following test skeleton to see how this all would look like:
@Test
public void shouldBeEmpty(){
...
}
Sometimes you like to test if an error is thrown, probably see if the check for meaningless input works. For example if you pop an empty stack there should be an error – and it’s a failure, if there is not! To test this you have to write the expected error right after @Test. It should look like this:
@Test (expected=AssertionError.class)
Now if and only if this error is raised the test will pass.
Last but not least I’m going to show you an example with a few tests. It’s a very simple and trivial one and it’s not a thing you should write tests for, especially not chained ones, but you should see how it’s supposed to work. In this example I’m only going to test the basic calculation functions with Integers. Therefore I’ll test addition, subtraction, multiplication and division should work correct and the result should always be an Integer.
Example:
public class CalculaterTest {
public int testValue;
@Before
public void setUp(){
testValue = 0;
assertEquals(testValue, 0);
}
@Test
public void shouldAddFive(){
testValue = testValue + 5;
assertEquals(testValue, 5);
}
@Test
public void shouldSubTwo(){
testValue = testValue + 5;
testValue = testValue - 2;
assertEquals(testValue, 3);
}
@Test
public void shouldMultWithFour(){
testValue = testValue + 5;
testValue = testValue - 2;
testValue = testValue * 4;
assertEquals(testValue, 12);
}
@Test
public void shouldDivWithTwo(){
testValue = testValue + 5;
testValue = testValue - 2;
testValue = testValue * 4;
testValue = testValue / 2;
assertEquals(testValue, 6);
}
@Test (expected=AssertionError.class)
public void shouldRaiseError(){
testValue = testValue + 5;
testValue = testValue - 2;
testValue = testValue * 4;
testValue = testValue / 2;
testValue = testValue / 4 * 3;
assertEquals(testValue, 4.5, 0.00000001);
}
}
As you can see a lot of the code has to be copied every time which leads to long and hard to maintain tests but next month I’ll write about a cool Framework to improve this tests and to write even better tests.
That’s it for almost this month, “How to Write Test in Pharo” will be online 31th of October.
Stay tuned and leave comments below.
cheers d3orn
No comments yet.