Bug 51140 - Response Assertion: add ability to set a specific error/failure message that is later shown in the Assertion Result
Summary: Response Assertion: add ability to set a specific error/failure message that ...
Status: RESOLVED FIXED
Alias: None
Product: JMeter - Now in Github
Classification: Unclassified
Component: Main (show other bugs)
Version: unspecified
Hardware: PC All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: JMeter issues mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-02 13:19 UTC by Eduard Tudenhoefner
Modified: 2018-01-10 12:51 UTC (History)
2 users (show)



Attachments
Suggestion for the realization (5.73 KB, text/plain)
2011-07-20 08:13 UTC, Eduard Tudenhoefner
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eduard Tudenhoefner 2011-05-02 13:19:01 UTC
A possibility should be provided that allows a user to define a specific error/failure message in a response assertion, which is then shown with the Assertion Result. By adding a specific message it is much easier to reproduce why an assertion resulted in an error/failure (e.g. by adding the state of particular variables that lead to that error/failure).
Comment 1 Eduard Tudenhoefner 2011-07-20 08:11:54 UTC
A suggestion for the realization would be as following:

Entry in message.properties:
view_results_assertion_specific_failure_message=Specific failure message: 

public class AssertionResult implements Serializable {
     /** The failure message which can be provided by the user. */   
    private String specificFailureMessage;
	
    public String getSpecificFailureMessage() {
		return specificFailureMessage;
	}

	public void setSpecificFailureMessage(String specificErrorMessage) {
		this.specificFailureMessage = specificErrorMessage;
	}
}

public class AssertionLogGui extends AssertionGui {
    
    private JTextField specificErrorMessage;
    
    /**
     * Create a new AssertionGui panel.
     */
    public AssertionLogGui() {
    	super();
    	JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("Save variable context into file (if activated in test scenario)"));
        panel.setLayout(new GridLayout(0,2)); 
        panel.add(new JLabel("File for success:"));
        successFile = new JTextField(30);
        panel.add(successFile);
        
        panel.add(new JLabel("File for failure:"));
        failFile = new JTextField(30);
        panel.add(failFile);
        
        panel.add(new JLabel("Variable pattern to write in file:"));
        patternFile = new JTextField(30);
        panel.add(patternFile);
        
        JPanel errorMessagePanel = new JPanel();
        errorMessagePanel.setBorder(BorderFactory.createTitledBorder("Specific Failure Message"));
        errorMessagePanel.setLayout(new GridLayout(0,1)); 
        specificErrorMessage = new JTextField("",100);
        errorMessagePanel.add(specificErrorMessage);
    	
        Box box = (Box)getComponent(0);
        box.add(oep, 1);
        box.add(panel, 2);
        box.add(errorMessagePanel, 3);        
    }
	
	 /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
    public void modifyTestElement(TestElement el) {
        super.modifyTestElement(el);
        
        if (el instanceof ResponseLogAssertion) {
        	ResponseLogAssertion ra = (ResponseLogAssertion) el;
        	ra.setFailVarFile(failFile.getText());
        	ra.setSuccessVarFile(successFile.getText());
        	ra.setVarFileString(patternFile.getText());
        	ra.setErrorAction(oep.getOnErrorSetting());
        	ra.setSpecificFailureMessage(specificErrorMessage.getText());
        }
    }
	
	@Override
    public void configure(TestElement el) {
        super.configure(el);
        ResponseLogAssertion model = (ResponseLogAssertion) el;

        failFile.setText(model.getFailVarFile());
        successFile.setText(model.getSuccessVarFile());
        patternFile.setText(model.getVarFileString());
        specificErrorMessage.setText(model.getSpecificFailureMessage());
        oep.configure(model.getErrorAction());
    }
}

public class ResponseAssertion extends AbstractScopedAssertion implements Serializable, Assertion {
   
    private static final String SPECIFIC_FAILURE_MESSAGE = "Assertion.specific_failure_message"; // $NON-NLS-1$
       
    public String getSpecificFailureMessage() {
    	return getPropertyAsString(SPECIFIC_FAILURE_MESSAGE);
    }
    public void setSpecificFailureMessage(String message) {
    	setProperty(SPECIFIC_FAILURE_MESSAGE, message);
    }
        
    /**
     * {@inheritDoc}
     */
    @Override
    public AssertionResult getResult(SampleResult response) {
		AssertionResult result;

        // None of the other Assertions check the response status, so remove
        // this check
        // for the time being, at least...
        // if (!response.isSuccessful())
        // {
        // result = new AssertionResult();
        // result.setError(true);
        // byte [] ba = response.getResponseData();
        // result.setFailureMessage(
        // ba == null ? "Unknown Error (responseData is empty)" : new String(ba)
        // );
        // return result;
        // }

        result = evaluateResponse(response);
		result.setSpecificFailureMessage(getSpecificFailureMessage());       
        return result;
    }
}

public abstract class SamplerResultTab implements ResultRenderer {

    @SuppressWarnings("boxing")
    public void setupTabPane() {
        StyledDocument statsDoc = stats.getStyledDocument();
        try {
            statsDoc.remove(0, statsDoc.getLength());
            sampleDataField.setText(""); // $NON-NLS-1$
            results.setText(""); // $NON-NLS-1$
            if (userObject instanceof SampleResult) {
                // ...           
            } else if (userObject instanceof AssertionResult) {
                assertionResult = (AssertionResult) userObject;

                // We are displaying an AssertionResult
                setupTabPaneForAssertionResult();

                StringBuilder statsBuff = new StringBuilder(100);
                statsBuff.append(JMeterUtils.getResString("view_results_assertion_error")).append(assertionResult.isError()).append(NL); //$NON-NLS-1$
                statsBuff.append(JMeterUtils.getResString("view_results_assertion_failure")).append(assertionResult.isFailure()).append(NL); //$NON-NLS-1$
                statsBuff.append(JMeterUtils.getResString("view_results_assertion_failure_message")).append(assertionResult.getFailureMessage()).append(NL); //$NON-NLS-1$
                if (null != assertionResult.getSpecificFailureMessage() && !"".equals(assertionResult.getSpecificFailureMessage())) {
                	statsBuff.append(JMeterUtils.getResString("view_results_assertion_specific_failure_message")).append(assertionResult.getSpecificFailureMessage()).append(NL); //$NON-NLS-1$
                }
                statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
            }
            
        } catch (BadLocationException exc) {
            stats.setText(exc.getLocalizedMessage());
        }
    }
}
Comment 2 Eduard Tudenhoefner 2011-07-20 08:13:47 UTC
Created attachment 27301 [details]
Suggestion for the realization
Comment 3 benoit.wiart 2016-01-22 14:48:59 UTC
This is a really useful feature.
Anybody knows why it wasn't merged ?
Comment 4 benoit.wiart 2016-01-22 15:00:25 UTC
I think a text area where you can add a message (with variable replacement) should be enough.
Comment 5 Philippe Mouawad 2018-01-10 12:51:21 UTC
Author: pmouawad
Date: Wed Jan 10 12:50:48 2018
New Revision: 1820745

URL: http://svn.apache.org/viewvc?rev=1820745&view=rev
Log:
Bug 51140 - Response Assertion: add ability to set a specific error/failure message that is later shown in the Assertion Result
Contributed by UbikLoadPack
Bugzilla Id: 51140

Modified:
    jmeter/trunk/src/components/org/apache/jmeter/assertions/ResponseAssertion.java
    jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/AssertionGui.java
    jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
    jmeter/trunk/src/core/org/apache/jmeter/resources/messages_fr.properties
    jmeter/trunk/test/src/org/apache/jmeter/assertions/ResponseAssertionTest.java
    jmeter/trunk/xdocs/changes.xml
Comment 6 The ASF infrastructure team 2022-09-24 20:37:46 UTC
This issue has been migrated to GitHub: https://github.com/apache/jmeter/issues/2489