I have the following Gherkin:

Scenario: A user can register to the application
  Given Carlos is not a registered user
  When Carlos registers on to the application
  Then he gets registered

My question has to do with the repetition of the test. What I mean is that I see this test failing if it runs for a second time. To make it more clear:

If we run the test the first time, Carlos is not registered in the app DB and as such he is registering and the outcome is that he gets registered in the app. The scenario for the first time will succeed.

The second time that we run the test, unfortunately, Carlos will be already registered on the app (DB) and as such the scenario will fail (as the given part will be invalid).

How do we solve such problems? Should we have some kind of a tear-up/tear-down DB or something?

5 Answers 5


Ideally, your tests should always run against the same configuration so they are 100% reproducible. This also includes the database.

There are two typical options to solve your issue:

  1. As mentioned, reset or clear your database before every test run. This ensures no duplicate validations (or other weird stuff) can mess with your tests and produce unwanted failures.
  2. Randomize your test data. In this case, replace Carlos with someone (you don't care what the exact name is) and generate a unique one every test run. Or, make the code behind Carlos append a unique value or id.

You could add a hook that would run prior to each test execution. And that hook would delete the user from the database for that particular scenario.

public void beforeScenario(Scenario scenario) { 
    // delete user from db

More info for Java Cucumber here.


You can have the @before and @after hooks. where the @before hook initiates the application and the @after hook destroys the user every after a scenario is completed. And hence you should not find the user in the db each time a scenario is run.

Refer to https://www.baeldung.com/java-cucumber-hooks


public void destroy() {

Otherwise you can use RandomStringUtils class to always randomly generate a string(each time a scenario is run) which could be used as a username henceforth a new user is created everytime the scenario is run


Insert random variables in test data to run multiple times

Whatever can create duplicates in your application , introduce random variables in your tests . For example each time a new user is registered its registered with new name and credentials using a random string function which returns a new unique name and credentials everytime.

Refresh db daily overnight

Also refresh your database daily overnight so that you don't have much junk or duplicate data and can run same tests every day without any data issues.


Remove the dependency to control the conditions.

Modify the application so that when it is run in test mode it uses mock responses from test data or a proxy server instead of the actual database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.