Jmeter + Junit

Source: Internet
Author: User

Before using the functionality of JUnit in JMeter, it is better to manually tune the interface to refer to the JMeter HTTP Request

Let's start by talking about how to use the JUnit feature

1. Write the test class Jmeterjunit code as follows

 PackageCom.test.junit;Import Staticjunit.framework.Assert.assertTrue;Import Staticorg.junit.assert.*;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test; Public classJmeterjunit { PublicString URLPath = "http://www.mocky.io/v2/572ee171120000332519491b";  PublicHttpURLConnection connection =NULL;  PublicOutputStream output =NULL;  PublicBufferedReader reader =NULL;  Public StaticString request = "{\" type\ ": \" getname\ "}"; @Before Public voidSetUp ()throwsException {URL url=NewURL (URLPath); Connection=(HttpURLConnection) url.openconnection (); Connection.setdooutput (true); Connection.setdoinput (true); Connection.setrequestmethod ("POST"); Connection.setusecaches (false); Connection.setinstancefollowredirects (true); Connection.setrequestproperty ("Content-type", "Application/json"); Connection.setrequestproperty ("Content-encoding", "UTF-8"); Connection.setrequestproperty ("Accept", "Application/json");    Connection.connect (); } @After Public voidTearDown ()throwsException {if(NULL!=output) {            Try{output.close (); } Catch(IOException e) {Throwe; }        }        if(NULL!=reader) {            Try{reader.close (); } Catch(IOException e) {Throwe; }        }        if(NULL!=connection)        {Connection.disconnect (); }    }         Public voidSendRequest (String request)throwsException {Output=Connection.getoutputstream ();        Output.write (Request.getbytes ());    Output.flush (); }     PublicString GetResponse ()throwsException {Reader=NewBufferedReader (NewInputStreamReader (Connection.getinputstream ()));        String St; StringBuffer STB=NewStringBuffer ();  while(NULL! = (St =Reader.readline ()))        {Stb.append (ST); }        returnstb.tostring (); } @SuppressWarnings ("Deprecation")     Public voidCheckresponse (String response,string name)throwsException {BooleanActualresult =response.contains (name); Asserttrue ("The expected status is" +name+ ", but now it's not", Actualresult); } @Test Public voidTest1 ()throwsException {sendrequest (request); Checkresponse (GetResponse (),"Brett"); } @Test Public voidTest2 ()throwsException {sendrequest (request); Checkresponse (GetResponse (),"Jason"); } @Test Public voidTest3 ()throwsException {sendrequest (request); Checkresponse (GetResponse (),"Elliotte"); }        }

The request address and response information in the code is described in the HTTP request for JMeter, which is not much to say here. In the code, three test methods were written to check if the responses contained the three names of Brett, Jason, and Elliotte.

2. Debug the use case with the following results

3. After debugging, export the jar package and import the jar package into the. \jmeter\apache-jmeter-2.13\lib\junit directory. (If a third-party jar package is introduced when writing code, the third-party jar package is placed in the. \jmeter\apache-jmeter-2.13\lib)

4. Restart JMeter, add three junit Request, view result tree, and graphical results.

5 Click the Run button, you can view the results in the view results tree, if you want to save the test results to a file, you can configure

Jmeter + Junit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.