tags:

views:

63

answers:

2

I'm new to developing Unit test and I can't find an example of how to test an existing class I have.

The class has a save method which does an insert or update in the database when the user clicks Save in the UI. But the save method has required fields that need to be populated. And has other fields that do not.

So how can I run this test properly?

Was trying to write it out..

Give a user
When user saves object
Then Field1 is required
then Field2 is required
Then Field3 is required

WhenUserSavesObject()
object = new object
object.field1 IsNot Nothing

something like that right?

And what about the other fields that are optional? How would I test the save method to make sure it takes all those values properly?

Was trying to use BDD but not sure if I should try it or not.

Can't find any example of classes with many properties that are needed when calling a test method.

+1  A: 

You did not tell in which language you want to implement your project and your tests. For example with Groovy & Grails the framework provides a wonderful toolset for implementing those tests easily and works great as an example on how to implement tests like the one you are asking for.

void testUsernameIsRequiredToValidate() {
  mockForConstraintsTests User
  def user = new User(username: "")
  assertFalse user.validate()
  assertEquals "blank", user.errors["username"]
}

The code is self explanatory. In the first line of the test method we are setting up the environment to add validation functionality to the User class within the unit test. After that we create a new User and validate it against it's constraints. The last line checks that a blank-constraint on the field firstname caused the test to fail.

class User {
  String firstname
  String password

  static constraints = {
    username(blank: false)
    password(blank: false, minSize: 6)
  }
}

Having multiple required fields to validate a User object could be implemented in several small classes for the negative testing. For positive testing you would build up a valid User instance and validate it.

void testUserWithUsernameAndAMinimumPasswordOfSixCharactersIsValid() {
  mockForConstraintsTests User
  def user = new User(username: "admin", password: "secret")
  assertTrue user.validate()
}

If you are developing with another framework and/or language you might be interested in the topic of mocking away dependencies and breaking down the code under test into small testable parts.

codescape
+1  A: 

If you are just learning unit tests, IMHO you could choose a simpler target for your first unit test. Unit testing with a DB is much more complex than testing a plain object without dependencies. It is better to practice and learn one thing at a time.

However, if you really want to tackle this, you should tell us what language, persistence mechanism, DB you are using - otherwise we are just shooting in the dark. E.g. if you happen to use Java, DbUnit is your friend.

It is generally good practice to limit dependencies on the DB to a thin layer within your code, so as to make the rest of your code easier to test. From your description it is not clear whether your class is already part of that layer or it can be insulated from the DB yet.

Péter Török
Aww I see. I'm learning some basics right now. Found a great book. http://www.artofunittesting.com/It's VB.Net and I'm using Microsoft's version of nUnit. The DB layer and business logic are on the same layer, in the same class.I think this book will actually help, as it's now getting into real world scenarios.! Thank you for help.
Mastro