'Adding Assertion module to HashTree from Jmeter using Java

Edit

The hirerachy of my test script is the following:

- test plan
  - thread group 1
    - http sampler 1-1

  - thread group 2
    - http sampler 2-1
- thread group 1
    - http sampler 1-1

  - thread group 3
    - http sampler 3-1

I can traverse all the thread groups. However, I can not use the similar pattern to traverse http sampler.

Here is my code:

public static void main(String[] args) throws IOException {
        // LOAD EXITISTING JMETER XML
        JmxReader jmxReader = new JmxReader(".\\resources\\manually-configure.jmx");
        HashTree testPlanTree = jmxReader.getTree();

        SearchByClass testPlanSearcher = new SearchByClass(TestPlan.class);
        SearchByClass threadGroupSearcher = new SearchByClass(ThreadGroup.class);
        SearchByClass httpSamplerSearcher = new SearchByClass(HTTPSampler.class);
        testPlanTree.traverse(testPlanSearcher);
        Iterator testPlanIter = testPlanSearcher.getSearchResults().iterator();

        while (testPlanIter.hasNext()) { // This loop will only execute once, due to we only have one test plan
            TestPlan testPlan = (TestPlan) testPlanIter.next();
            HashTree subTreeOfTestPlan = testPlanSearcher.getSubTree(testPlan);
            subTreeOfTestPlan.traverse(threadGroupSearcher);
            Iterator threadGroupIter = threadGroupSearcher.getSearchResults().iterator();
            while(threadGroupIter.hasNext()) {
                ThreadGroup threadGroup = (ThreadGroup) threadGroupIter.next();
                HashTree subTreeOfThreadGroup = threadGroupSearcher.getSubTree(threadGroup);
                subTreeOfThreadGroup.traverse(httpSamplerSearcher);
                Iterator httpSamplerIter = httpSamplerSearcher.getSearchResults().iterator();

                while (httpSamplerIter.hasNext()) {
                    httpSamplerIter.next();
                    System.out.println("I found a http sampler"); \\ ??? Nothing got printed
                }
            }
         }
    }

The third inner loop for traverse the http smaplers does not execute. However, there are at least one sampler within each thread group for sure.



Solution 1:[1]

You basically need to instantiate ResponseAssertion and add it wherever you need, for example to the root of the test plan:

testPlanTree = SaveService.loadTree(in);

ResponseAssertion responseAssertion = new ResponseAssertion();
responseAssertion.setName("Response Assertion");
//configure the assertion according to your requirements
responseAssertion.setProperty(TestElement.TEST_CLASS, ResponseAssertion.class.getName());
responseAssertion.setProperty(TestElement.GUI_CLASS, AssertionGui.class.getName());

testPlanTree.add(responseAssertion);

SaveService.saveTree(testPlanTree, new FileOutputStream("/path/to/script/with/assertion.jmx"));

More information: Five Ways To Launch a JMeter Test without Using the JMeter GUI

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T