package ru.stqa.pft.addressbook.tests;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ru.stqa.pft.addressbook.model.GroupData;
import ru.stqa.pft.addressbook.model.Groups;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class GroupDeletionTests extends TestBase {

    @BeforeMethod
    public void ensurePrecondition() {
        app.navigateTo().groupPage();
        if (! app.group().doesExist()) {
            app.group().create(new GroupData().withName("test"));
        }
    }

    @Test
    public void testGroupDeletion() {

        Groups groupsBefore = app.group().all();
        GroupData deletedGroup = groupsBefore.getRandom();

        app.group().deleteById(deletedGroup.getId());

        // assertions
        assertThat(app.group().getGroupCount(), equalTo(groupsBefore.size() - 1));
        Groups groupsAfter = app.group().all();
        assertThat(groupsAfter, equalTo(groupsBefore.without(deletedGroup)));
    }

}

package ru.stqa.pft.addressbook.tests;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ru.stqa.pft.addressbook.model.GroupData;
import ru.stqa.pft.addressbook.model.Groups;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class GroupDeletionTests extends TestBase {

    @BeforeMethod
    public void ensurePrecondition() {
        if (app.db().groups().size() == 0) {
            app.navigateTo().groupPage();
            app.group().create(new GroupData().withName("test"));
        }
    }

    @Test
    public void testGroupDeletion() {
        app.navigateTo().groupPage();
        Groups groupsBefore = app.db().groups();
        GroupData deletedGroup = groupsBefore.getRandom();

        app.group().deleteById(deletedGroup.getId());

        // assertions
        assertThat(app.group().getGroupCount(), equalTo(groupsBefore.size() - 1));
        Groups groupsAfter = app.db().groups();
        assertThat(groupsAfter, equalTo(groupsBefore.without(deletedGroup)));
    }

}

add db checking to group deletion tests