Natural Order Development

Copyright © 2008 by Leeland Artra
You are not logged in.
Login
Register



A Django site.

Tag: testing / Back

Unit Testing Java

2010-07-20 10:57:24

Basic testing patterns are important. But it is also important to properly plan the testing. Building a matrix really helps make sure you are covering all the basis.

A simple case is something that will compare two items for you. These little helper classes need to be built all the time for sorted lists and other such uses. However, it is amazingly easy to get something out of alignment. So for argument sake lets stick with something simple: "Compare two strings as being equal, allowing nulls to be used such that 2 nulls return true."

So the comparison for these would be based off of this matrix:

[table]
[tr] [td]Input 1[/td] [td]Input 2[/td] [td]Result[/td] [/tr]
[tr] [td]null[/td] [td]null[/td] [td]true[/td] [/tr]
[tr] [td]"A"[/td] [td]null[/td] [td]false[/td] [/tr]
[tr] [td]null[/td] [td]"B"[/td] [td]false[/td] [/tr]
[tr] [td]"A"[/td] [td]"B"[/td] [td]false[/td] [/tr]
[tr] [td]"A"[/td] [td]different "A"[/td] [td]true[/td] [/tr]
[/table]

Now you might think that was the end of it. However, now it is important to examine the implementation.

The question is should "A" == "A" be allowed? Maybe yes maybe no. Lets assume we want the answer to be no (we do not want to allow the same object to be considered equal to itself).

Therefore, we need to add one more test to the plan, specifically:

[table]
[tr] [td]Input 1[/td] [td]Input 2[/td] [td]Result[/td] [/tr]
[tr] [td]"A"[/td] [td]same object "A"[/td] [td]false[/td] [/tr]
[/table]

assertTrue(equalsAllowNull(null, null));
assertFalse(equalsAllowNull(null, "A"));
assertFalse(equalsAllowNull("A", null));
assertFalse(equalsAllowNull("A", "B"));
// new() to prevent compiler from reusing same instance...
assertTrue(equalsAllowNull("A", new String("A"))); 
// confirm compiler is not reusing the object "A" using .equals and not ==
assertFalse("A" == new String("A"));
assertTrue("A" == "A");
// confirm the function using .equals and not ==
assertFalse(equalsAllowNull("A", "A"));


And now the actual function:

public boolean equalAllowNull(String a, String b) {

  return a == null ? b == null : a == b ? false : a.equals(b);

}



Posted by Leeland

Assigned Tags: Java, unit testing, testing

0 Comments

How to Write 3v1L, Untestable Code

2008-07-25 19:51:20

I always love a good laugh and this one is great. The developers and testers at Google have put up a great "How to Write 3v1L, Untestable Code" article with tongue in cheek and enough sarcasm to really tickle your thought processes. In reality is a list of things not to do as a developer. I love the reverse style delivery. You can find the complete post here: Google Testing Blog (http://googletesting.blogspot.com/2008/07/how-to-write-3v1l-untestable-code.html).

Posted by Leeland

Assigned Tags: code quality, best practices, testing

0 Comments

Backup and restore worked

2008-06-05 16:33:13

Well in a moment of testing need I decided to determine if the backups where working. So I took a backup of the database and restored it to an entirely different database service. I altered the database handler to point at the new database service and kicked the web services.

What a surprise it actually all came back up just like it is supposed to.

I am in shock. Not only are all the forums back in place, the posts are there, the blogs are there and all the test user data is there too.

I call that one heck of a test. I am feeling a lot more confident in this process now. Especially since I moved this from a testing database to a production database service. ;-)

Excellent test of the services, the automation and the disaster recovery process.

Not to mention my testing database service can now be redirected to more serious poking and prodding for the layout of all the database models used for this service.

Posted by Leeland

Assigned Tags: nodsw, testing

0 Comments

Page 1

Archive

RSS Feed



Powered by Sphene Community Tools