René Schwietzke, Xceptance
The big picture about this lecture.
Feel free to ask any question, anytime.
What you should know before we start.
Rather ask too much than too little.
Programming is like writing books. Not every book is good, not every book will make you famous, not every book will earn you enough.
But a lot of people think they can write a book... cannot be that hard, can it?
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
c b a
http://creativecommons.org/licenses/by-sa/4.0/
In 5 minutes
Couple of lines that show the basics.
package org.training.quick;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FeelGood
{
/**
* Returns the max number.
* @param a value one
* @param b value two
* @return the maximum of a and b
*/
private static int max(final int a, final int b)
{
if (a < b)
{
return b;
}
else
{
return a;
}
}
@Test
public final void test()
{
assertEquals(3, max (1, 3));
}
}
No learning without topic.
Simple data structure with enough functionality but also with enough room for testing and extensions.
java.util.ArrayList
HashMaps are cooler, but require more algorithmic upfront knowledge.
Steps to approach the programming and testing topic.
Testing and coding is normally one thing or even better, testing comes before coding.
Let the brain do the work first.
A dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed.
In comparison, an array is statically sized.
What is the minimal set of functionality that is needed?
Strings only for now.
Question you should immediately ask yourself.
null
permitted?We will talk about the details later. Just keep it in mind.
Align us to speed up programming.
Verify all of that
IDE is a thing of personal preference or employer dictat.
Programming our ideas.
Step-by-step
Order is not fix and do not write test code in your class!
Test the hell out of your code!
How to test code.
Testing with JUnit might not be unit testing.
/**
* Removes a string from the list by a given position. If the
* position does not exist, an ArrayIndexOutOfBoundsException is
* thrown.
*
* @param pos the position to remove
* @exception ArrayIndexOutOfBoundsException
* when pos is invalid such as negative or too large
* @return the string that has been removed from the list
*/
public String remove(final int pos)
{
// get the result
final String result = array[pos];
// we have to close the hole
for (int i = pos; i < size - 1; i++)
{
array[i] = array[i + 1];
}
// to be really great to GC, we have to make sure we do not hold
// a copy of the last element at the end, this position should be
// empty now. Make it so!
array[size - 1] = null;
// correct size
size--;
// return the result
return result;
}
/**
* Remove last element
*/
@Test
public void removeLastListEntry()
{
final ArrayList list = new ArrayList();
list.add("s1");
final String r1 = list.remove(0);
Assert.assertEquals(0, list.size());
Assert.assertEquals("s1", r1);
}
/**
* Error handling, remove from empty
*/
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void removeErrorHandling1()
{
final ArrayList list = new ArrayList();
list.remove(0);
}
One test case or test topic, one test method.
No strictly enforced.
If it does not run twice, remove it.
Everything beyond this class.
You can test a lot.
@Test
/**
* Remove last element
*/
@Test
public void removeLastListEntry()
{
final ArrayList list = new ArrayList();
list.add("s1");
final String r1 = list.remove(0);
Assert.assertEquals(0, list.size());
Assert.assertEquals("s1", r1);
}
org.junit.Assert
/**
* Test adding the same data again
*/
@Test
public void addHandlingDuplicateData()
{
final String A = "foo1";
final String B = new String("foo1");
// this is the string challenge :)
Assert.assertNotSame(A, B);
Assert.assertEquals(A, B);
final ArrayList list = new ArrayList();
Assert.assertEquals(0, list.size());
}
If you cannot assert it, you cannot test it.
@Test (expect = Exception.class)
/**
* Test error handling
*/
@Test(expected=NullPointerException.class)
public void addErrorHandling()
{
final ArrayList list = new ArrayList();
list.add((String)null);
}
@Role is another one... more advanced.
@After, @Before...
/**
* Setup data for the test once
*/
@BeforeClass
public void createSomethingOnce() {}
/**
* Setup data for the test before each method
*/
@Before
public void createSomething() {}
/**
* Clean up after each test
*/
@After
public void removeStuff() {}
/**
* Clean up stuff once
*/
@After
public void removeStuffOnce() {}
@Role is another one... more advanced.
Measure your testing.
Coverage is nice, but a limited measure.