0

i want to add an object in DB than check if the object is already there so we can't add it twice. I used JUNIT to test it:

@org.junit.Test (expected = ExistingProduct.class)
public void AddExisting() 
{

    Register aRegister = new Register();
    Product aProduct = new Product();
    aProduct.setPIN("079400027252"); 

    aRegister.AddProduct(aProduct);


    Product sameProduct = new Product();
    sameProduct.setPIN("079400027252");

    aRegister.AddProduct(sameProduct); //this throw the exception


    aRegister.deleteProduct("079400027252"); //CAN'T REACH HERE
}

The problem is that i can't delete the product since the instruction that's called before will throw a exception thus end the test.

2 Answers2

0

well i sort of figure out something... I call the @Before method witch delete all from the BD and put back default value

0

Maybe something like this solving this problem. More is on https://stackoverflow.com/a/20494165/4296891 by Sergey Berezovskiy

public void AddExisting() 
    {

        Register aRegister = new Register();
        Product aProduct = new Product();
        aProduct.setPIN("079400027252"); 

        aRegister.AddProduct(aProduct);


        Product sameProduct = new Product();
        sameProduct.setPIN("079400027252");
        try
        {
            aRegister.AddProduct(sameProduct); //this throw the exception
            assertEquals("Can't add it twice exception has been excepted." ,true, false);
        }
        catch(ExistingProduct exception)
        {        
            // exception
        }

        aRegister.deleteProduct("079400027252"); //CAN'T REACH HERE
    }
Community
  • 1
  • 1
Jiri Sykora
  • 484
  • 4
  • 10