# Assertion on the HTTP status code

**URL:** <https://community.gatling.io/t/assertion-on-the-http-status-code/7355>\
**Category:** Gatling (Open-Source)\
**Created:** [November 29, 2022, 2:04pm UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355 "2022-11-29T14:04:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukas.troellinger](https://avatars.discourse-cdn.com/v4/letter/l/bc79bd/32.png) [@lukas.troellinger](https://community.gatling.io/u/lukas.troellinger)\
**Post date:** [November 29, 2022, 2:04pm UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/1 "2022-11-29T14:04:17Z")

</div>

Hello to all!

I have the following use case:

- I want to run a simulation (or maybe a set of simulations in the future) and assert if the simulation ran “successful” or not
- I define “Successful” as = no request got an HTTP Status Code 400 returned
- If there is a single HTTP Status code 400, the simulation should fail (e.g. maven should provide a “Build failure” or a exit code 1)

Maybe the use case seems awkward, but my plan is to publish a successful run further to my colleagues. If the simulation has an error (aka a 400 error was returned), it should not be published any further.

On the assertion page [here](https://gatling.io/docs/gatling/reference/current/core/assertions), I found, that there could be assertions, but those target “only” on the times/percentiles.

Do you know a solution for this?

Best regards!

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 29, 2022, 5:29pm UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/2 "2022-11-29T17:29:49Z")

</div>

> On the assertion page [here](https://gatling.io/docs/gatling/reference/current/core/assertions), I found, that there could be assertions, but those target “only” on the times/percentiles.

No they don’t.

`global().failedRequests().count().is(0)`

Your 400 responses will trigger failures as explained [here](https://gatling.io/docs/gatling/reference/current/http/check/#http-status).

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [November 29, 2022, 8:56pm UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/3 "2022-11-29T20:56:09Z")

</div>

I have created example for Your Case:  
[Case0019WhenStatusCode400ThenFailSimulation](https://github.com/gemiusz/gatling-examples-maven-java/blob/master/src/test/java/pl/gemiusz/Case0019WhenStatusCode400ThenFailSimulation.java)

I forgot exclude it so my build is failed on GitHub 🤣

> <https://github.com/gemiusz/gatling-examples-maven-java/actions/runs/3577924455>

---

<div class="post-metadata">

**Author:** ![lukas.troellinger](https://avatars.discourse-cdn.com/v4/letter/l/bc79bd/32.png) [@lukas.troellinger](https://community.gatling.io/u/lukas.troellinger)\
**Post date:** [December 1, 2022, 8:56am UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/4 "2022-12-01T08:56:50Z")

</div>

Hello!

Thank you very much @slandelle and @GeMi for your help and your support!

Your solution counts HTTP 5xx errors as “failed requests” too. But I do not want to consider those requests-responses, because those errors are not caused by my Gatling simulation but by the BE or infrastructure. So my intention is, if my simulation does not produce any 4xx errors, I continue with further steps.

Hmmm 🤔 … yesterday I found this solution in _powershell_ though:

```auto
$lastrun=Get-Content ./target/gatling/lastRun.txt
$nomatches=(Get-Content ./target/gatling/$lastrun/simulation.log | Select-String -Pattern 'found 4\d{2}').Matches.Length
if ($nomatches -gt 0) { 
  exit 1
}

```

So, if a 4xx error is logged in the last ‘simulatioin.log’ file, I exit the pipeline respectively the GitHub Action. Feels hacky, but it works quite well so far.

I’ll let you know if I can come up with a better solution. But for now, I leave everything like it is.

Thanks guys for your help … and also for this great community here!  
Kind regards!

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [December 1, 2022, 11:24am UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/5 "2022-12-01T11:24:30Z")

</div>

> [@lukas.troellinger](#):
>
> Your solution counts HTTP 5xx errors as “failed requests” too

If you look in [@GeMi code](https://github.com/gemiusz/gatling-examples-maven-java/blob/master/src/test/java/pl/gemiusz/Case0019WhenStatusCode400ThenFailSimulation.java#L26), you’ll see that only 400 will fail the request thank to `.check(status().not(400))`.

Does that help you?

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [December 31, 2022, 11:25am UTC](https://community.gatling.io/t/assertion-on-the-http-status-code/7355/6 "2022-12-31T11:25:22Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
