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