What is the best approach for testing negative scenarios?

611 views
Skip to first unread message

Moshe Elisha

unread,
Mar 20, 2016, 5:16:17 PM3/20/16
to Serenity BDD Users Group
Hi,

Can you please recommend a way to perform negative testing using Serenity BDD and JBehave?

My current idea is to implement something similar to the JUnit ExpectedException rule.

For example:

Given the next step should throw com.nokia.cb.nfvo.mistral.client.exception.MistralHttpException while message contains ActionExecution not found
Then I update the Action Execution Id 12345 with state ERROR

But in order to implement this - I think code changes are needed in JBehave (Allow a hook to catch an exception in org.jbehave.core.steps.StepCreator.ParametrisedStep.perform).

Thanks.

Selva kumar V

unread,
Mar 31, 2016, 5:56:11 AM3/31/16
to Serenity BDD Users Group
Hi Moshe,
Can you try something with @rule for  Custom Exceptions With JUnit's ExpectedException. Just a thought 

Moshe Elisha

unread,
Mar 31, 2016, 5:48:09 PM3/31/16
to Serenity BDD Users Group
Can you please elaborate?

adi....@outlook.com

unread,
Sep 6, 2016, 8:27:04 AM9/6/16
to Serenity BDD Users Group
You can always surround the code you want to throw the error in a try/catch, in the catch just pass on the error status code and check it after if it is the expected code

For e.g.:

HttpStatus errorStatus;
try {
    // do a post with invalid params
} catch (HttpClientErrorException e) {
  errorStatus = e.getStatusCode();
}

assertEquals(HttpStatus.BAD_REQUEST, errorStatus);

Moshe Elisha

unread,
Sep 6, 2016, 11:15:56 AM9/6/16
to adi....@outlook.com, Serenity BDD Users Group
Thank you, yes but then I can't reuse the same step for positive and negative tests...

--
You received this message because you are subscribed to a topic in the Google Groups "Serenity BDD Users Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/thucydides-users/7MEOni_aHNc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to thucydides-users+unsubscribe@googlegroups.com.
To post to this group, send email to thucydides-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Smart

unread,
Sep 6, 2016, 11:46:33 AM9/6/16
to Moshe Elisha, adi....@outlook.com, Serenity BDD Users Group
You typically wouldn't be reasoning in terms of exceptions that are thrown in JBehave or Cucumber, it would more be expressed in terms of the visible outcomes for the user (e.g. "Then no new customer should be registered" vs "Then a CustomerAlreadyExists exception is thrown"). You can catch the exceptions (or error codes) inside the steps, and reuse the underlying steps for different cases. 

--
You received this message because you are subscribed to the Google Groups "Serenity BDD Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to thucydides-users+unsubscribe@googlegroups.com.

To post to this group, send email to thucydides-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
___________________________________________________
John Smart | Wakaleo Consulting  |  +44 7398 832273
Making smart teams collaborate better
http://johnfergusonsmart.com  |  john....@wakaleo.com
___________________________________________________

We love breaking down silos and helping smart teams collaborate better! Ask about our tailored on-site workshops in Agile Product Planning, BDD Requirements Discovery,  BDD, TDD and Clean Coding, and Advanced BDD Test Automation.
___________________________________________________

adi....@outlook.com

unread,
Sep 7, 2016, 2:51:32 AM9/7/16
to Serenity BDD Users Group
You can use the same step but you need to adjust a bit:
For e.g.

@When("I try and set the status to $status)
public void tryAndSetStatus(String statuts) {
// do some stuff you need
 try {
     // do a post with invalid params
     ResponseEntity<UserResource>  updatedUserResponse = // your post with status
     Serenity.setSession("updatedUser").to(updatedUserResponse.getBody());
     Serenity.setSession("statusCode").to(updatedUserResponse.getStatusCode());
 } catch (HttpClientErrorException e) {
   Serenity.setSession("statusCode").to(getStatusCode());
 }
}

@Then("The status is changed correctly")
public void thenTheStatusIsChangedCorrectly() {
   HttpStatus statusCode = (HttpStatus) Serenity.getCurrentSession().get("statusCode");
   assertEquals(HttpStatus.OK, statusCode);
  // other asserts ... probably on the updatedUser resource data
}

@Then("The status change request fails with a bad request")
public void thenIGetABadRequest() {
  HttpStatus statusCode = (HttpStatus) Serenity.getCurrentSession().get("statusCode");
   assertEquals(HttpStatus.BAD_REQUEST, statusCode);
}

I don't know if this is exactly what you are looking for but you can user the when step with any of the 2 then steps and depending on the param you send you can get a positive or a negative test case. Hope this helps.
To unsubscribe from this group and all its topics, send an email to thucydides-use...@googlegroups.com.
To post to this group, send email to thucydid...@googlegroups.com.

Moshe Elisha

unread,
Sep 17, 2016, 8:03:05 AM9/17/16
to Serenity BDD Users Group
Yes. It is also a good possibility. Thanks.
Reply all
Reply to author
Forward
0 new messages