0

I'm trying to write a TestNG to test db update for a property.Pseudocode is as below.

@Parameters({"newValue"})
@Test
public void testUpdateValue(@Optional String newValue)
{
    String originalValue = getValueFromDataBase(); //get Original value
    updateValue(newValue);   //update property to newValue
    String updatedValue = getValueFromDataBase();    //get updated value from db
    Assert.assertEquals(updatedValue == newValue, true);   //test value updated correctly.
    updateValue(originalValue);     // reset to origal value after test
}

am I using the correct method to test the db update? or Any other solutions are there in testNG?

Thanks in advance.

juherr
  • 5,422
  • 1
  • 18
  • 58
Ganesh D
  • 55
  • 1
  • 7

2 Answers2

0

It looks some thing not good at assertion.

   Assert.assertEquals(actual, expected);

for assert equals you need to provide actual value from db and your expected value. If both are equal then it will pass else fail.

   Assert.assertEquals(updatedValue, newValue);

Thank You, Murali

murali selenium
  • 3,637
  • 2
  • 9
  • 18
0

There are 3 problems I see with this test:

  1. As mentioned by murali, you should do Assert.assertEquals(updatedValue, newValue). This ensures that your assertion errors are useful. If you do Assert.assertEquals(updatedValue == newValue, true), then your message is going to be "False is not equal to True", instead of "'Joe' is not equal to 'Bob'".

  2. Your test depends on getValueFromDatabase() working correctly. If getValueFromDatabase() returns a random value each time, or the same value, or appends something, then this test will fail, even if updateValue() works.

  3. If updateValue() doesn't work, then you are leaving your database in a dirty state.

To solve problems 2 and 3, I recommend Mocking your database. What this entails:

  1. It looks like all of your database methods are static. This is a bad thing. Wrap your database in an object, and use object methods to hit your database.
  2. Your Database object would ideally take parameters in its constructor that define which network it uses.
  3. Create a mock object that represents a network (that you passed to your constructor above). It'll have the same methods as a normal network, but it won't actually send any requests.
  4. Then, in your test, you can test to see if dataBase.updateValue() sends an UPDATE across the network.

More info on mocking

Community
  • 1
  • 1
Nathan Merrill
  • 6,018
  • 5
  • 32
  • 49