You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMeter 4.0中If Controller取反条件不触发SMTP Sampler问题排查

Troubleshooting If Controller Not Triggering SMTP Sampler on Failure

I’ve run into this exact issue before with JMeter’s If Controller—let’s break down why this is happening and how to fix it.

Root Cause

The problem lies in how the If Controller evaluates your condition by default. When you use !JMeterThread.last_sample_ok directly in the Condition field:

  • The If Controller uses JavaScript to evaluate the expression by default.
  • JavaScript doesn’t have access to JMeter’s internal JMeterThread.last_sample_ok variable. So it treats this as an undefined value, which evaluates to false—that’s why your SMTP Sampler never runs when the assertion fails.

Even though your BeanShell Listener correctly sees !JMeterThread.last_sample_ok as true, BeanShell has direct access to JMeter’s context variables, which JavaScript doesn’t in this mode.

Fixes to Try

Option 1: Use Groovy Function (Simplest & Most Reliable)

Groovy integrates seamlessly with JMeter’s internal variables, so this is my go-to solution:

  1. Open your If Controller’s settings.
  2. In the Condition field, replace your current expression with:
    ${__groovy(!JMeterThread.last_sample_ok,)}
    
  3. Leave the "Interpret Condition as Variable Expression?" checkbox unchecked.

This works because the __groovy() function evaluates the negation of JMeterThread.last_sample_ok directly, returns a string ("true" or "false"), and the If Controller correctly interprets that as a boolean condition.

Option 2: Use JavaScript with JMeter’s vars Object

If you prefer sticking with JavaScript, you can access the variable via JMeter’s vars object (which is available in the JavaScript context):

  1. In the Condition field, enter:
    !(vars.get("JMeterThread.last_sample_ok") === "true")
    
  2. Leave the checkbox unchecked.

This explicitly fetches the string value of JMeterThread.last_sample_ok, checks if it equals "true", then negates the result—so it will be true when your assertion fails.

Option 3: Enable Variable Expression Mode with JEXL3

Another approach is to use JMeter’s variable expression mode with the JEXL3 function:

  1. Check the Interpret Condition as Variable Expression? checkbox.
  2. In the Condition field, enter:
    ${__jexl3(!${JMeterThread.last_sample_ok},)}
    

The __jexl3() function parses the variable’s string value into a boolean, negates it, and returns the result—this works because the If Controller now treats the condition as a variable expression instead of JavaScript.

Quick Verification

After applying any of these fixes:

  • Stop your service to trigger the assertion failure.
  • Run the test plan—your SMTP Sampler should now execute and send the email as expected.

内容的提问来源于stack exchange,提问作者Travis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:38:36