As I said in the question, is it possible to pass jmeter variables (passed through -J) into a JUnit Request in order that it can be used internally to the junit test?
It sounds feasible as the Apache Junit Request docs indicate that:
... JMeter currently runs the test methods directly, rather than
leaving it to JUnit. ...
This would indicate that jmeter could control passing parameters but I've found no supporting documentation and there's no obvious mechanism through the JUnit Request sampler config.
Background
I was hoping to use CSV Data Set Config to load a user pool (as in Victor Klepikovskiy excellent top tips) and have a randomly obtained user provided to my unit-tests (for use across the thread/ loop). I've found an alternative way of doing what I want (but use my own user pool accessed from within the junit tests). I know you could manage the login through Http Request too (as above) but we amanage the rest-service calls internally to junit so that they can be run as junit tests as well. I was interested in being as non-intrusive as possible.
A second reason for doing so might be to have a single junit test method but have it parameterised to suit the scenario.
Addendum
For what it's worth, I'm happy with my current alternative approach but still interested in how you might be able to do this for future reference. I'm fairly new to jmeter but expect to see a fair bit more of it. Was also thinking of looking at the pre-processors but not sure how these could interface with the Junit Request.
I have tried to do exactly what you are doing (although, never specifically used -J).
The short answer is I have still not found a way to make it work to pass variables into a JUnit Request. I believe it is not currently supported.
At the time, the only way I saw of doing this was passing a parameter (e.g., ${__threadNum} or even from CSV Data Set Config) into the Constructor String Label and writing my constructor appropriately. However, I only got the string literal "${__threadNum}". I contacted support and they mentioned to me that this will not work. I forget exactly what they said and cannot find the email. But the main idea was that a JUnit Request is instantiated or created before the parameter in the Conostructor String Label is parsed. So that is why it will not work.
I am currently using the same alternative you are by having my test read credentials from a file.
I tested and it all works. what needs to be done is:
Source: blazemeter.com how-use-junit-jmeter
1) add the following jar files in the project libraries:
..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_core.jar
..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_junit.jar
2) in the class, add the reference to the sampler:
import org.apache.jmeter.protocol.java.sampler.JUnitSampler;
3) Add the code in the #Test tag to handle variables:
Click to see Code
4) Define variables in JMeter (both in user define variables and CSV)
Click to see JMeter Config
Hope it helps!
Related
In my karate tests i need to write response id's to txt files (or any other file format such as JSON), was wondering if it has any capability to do this, I haven't seen otherwise in the documentation. In the case of no, is there a simple JavaScript function to do so?
Try the karate.write(value, filename) API but we don't encourage it. Also the file will be written only to the current "build" directory which will be target for Maven projects / stand-alone JAR.
value can be any data-type, and Karate will write the bytes (or plain-text) out. There is no built-in support for any other format.
Here is an example.
EDIT: for others coming across this answer in the future the right thing to do is:
don't write files in the first place, you never need to do this, and this question is typically asked by inexperienced folks who for some reason think that the only way to "save" a response before validation is to write it to a file. No, please don't waste your time - and please just match against the response. You can save it (or parts of it) to variables while you make other HTTP requests. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice. Also note that by default, Karate will dump all HTTP requests and responses in the log file (typically in target/karate.log) and also in the HTML report.
see if karate.write() works for you as per this answer
write a custom Java (or JS function that uses the JVM) to do what you want using Java interop
Also note that you can use karate.toCsv() to convert JSON into CSV if needed.
My justification for writing to a file is a different one. I am using karate explicitly to implement a mock. I want to expose an endpoint wherein the upstream system will send some basic data through json payload using POST/PUT method and karate will construct the subsequent payload file and stores it the specific folder, and this newly created payload file will be exposed through another GET call.
I have a complicated API request that needs to pass JSON data to an Ignite ComputeTask, but I can only seem to pass in data through the URL query string, which seems awkward, and potentially limiting. I have two questions:
Does the Ignite REST API have a max GET request limit, and if so, is there a way to increase it?
Is there any way to pass in JSON data through a POST request? I've experimented with ConnectorMessageInterceptor, but the args parameter is just the value of p1 from the query string.
If you're OK with passing JSON data in as a GET parameter, you can set the max GET size in your jetty configuration in your connector configuration using <Set name="requestHeaderSize">BYTES</Set>, though this is clearly not an optimal solution.
The short answer is no, there's no built-in way to intercept JSON POST body data in Ignite's built-in REST API. Although the Ignite documentation suggests that you configure Jetty's handlers, Ignite 2.7's implementation of Jetty (see GridJettyRestProtocol) actually overrides the configured Handler with its own GridJettyRestHandler, which only accepts requests in the form /ignite?cmd=cmdName&p1=params&name=taskName. To work around this, you can drop the ignite-rest-http lib and roll your own jetty implementation. If that seems like too much work and don't mind a somewhat hacky solution, you can piggyback on ignite's optional lib structure, and copy in just the file org.apache.ignite.internal.processors.rest.protocols.http.jetty.GridJettyRestProtocol from the ignite-rest-http lib, which Ignite will automatically pick up at start up. In GridJettyRestProtocol swap out GridJettyRestHandler for your own custom AbstractHandler that accepts POST data. Remember to import jetty as a project dependency.
You can supply your own Jetty config, which will probably let you configure GET request limit.
Have you actually tried to do POST with application/x-www-form-urlencoded inside?
I'm working on a codebase that attempts to make a REST call to a remote service, which returns JSON. The call works fine when it gets normal JSON back. In some cases, like when the network blows up, it gets back HTML, which tries to report various network problems.
Our error handling code in the client needs to deal properly with this situation, which is defined as simply obtaining the HTML string and logging it.
I find it's a little hard to write an automated test for this.
In our client code, we use ClientBuilder.newBuilder()... , then call "build()", which returns the Client, and then we call "client.target(url)" and then move forward with getting the response. We have an exception handler that will attempt to read the raw text.
In a test for this kind of thing, I'm used to using the CXF JAXRSServerFactoryBean class to run a server. I know how to connect the client to the server, but I'm not sure what to configure in the server to allow me to return arbitrary text, instead of a "resource class".
It's also possible that using JAXRSServerFactoryBean is overkill for this, as it just needs to return plain text.
What are my options here?
Update:
I did make progress setting up a test using JAXRSServerFactoryBean, but I'm still wondering if there are other options, especially ones that mitigate the next problem that I discovered, described at Using javax.ws.rs.client.ClientBuilder in CXF to create Client, any route to be able to use local transport? .
Update:
I gave up on the JAXRSServerFactoryBean approach, as I don't need to test the network, and doing so adds additional complications. The JAX-RS filter approach is simpler and avoids those complications. See the above linked question for details.
As I said in the question, is it possible to pass jmeter variables (passed through -J) into a JUnit Request in order that it can be used internally to the junit test?
It sounds feasible as the Apache Junit Request docs indicate that:
... JMeter currently runs the test methods directly, rather than
leaving it to JUnit. ...
This would indicate that jmeter could control passing parameters but I've found no supporting documentation and there's no obvious mechanism through the JUnit Request sampler config.
Background
I was hoping to use CSV Data Set Config to load a user pool (as in Victor Klepikovskiy excellent top tips) and have a randomly obtained user provided to my unit-tests (for use across the thread/ loop). I've found an alternative way of doing what I want (but use my own user pool accessed from within the junit tests). I know you could manage the login through Http Request too (as above) but we amanage the rest-service calls internally to junit so that they can be run as junit tests as well. I was interested in being as non-intrusive as possible.
A second reason for doing so might be to have a single junit test method but have it parameterised to suit the scenario.
Addendum
For what it's worth, I'm happy with my current alternative approach but still interested in how you might be able to do this for future reference. I'm fairly new to jmeter but expect to see a fair bit more of it. Was also thinking of looking at the pre-processors but not sure how these could interface with the Junit Request.
I have tried to do exactly what you are doing (although, never specifically used -J).
The short answer is I have still not found a way to make it work to pass variables into a JUnit Request. I believe it is not currently supported.
At the time, the only way I saw of doing this was passing a parameter (e.g., ${__threadNum} or even from CSV Data Set Config) into the Constructor String Label and writing my constructor appropriately. However, I only got the string literal "${__threadNum}". I contacted support and they mentioned to me that this will not work. I forget exactly what they said and cannot find the email. But the main idea was that a JUnit Request is instantiated or created before the parameter in the Conostructor String Label is parsed. So that is why it will not work.
I am currently using the same alternative you are by having my test read credentials from a file.
I tested and it all works. what needs to be done is:
Source: blazemeter.com how-use-junit-jmeter
1) add the following jar files in the project libraries:
..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_core.jar
..\JMeter\apache-jmeter-3.0\lib\ext\ApacheJMeter_junit.jar
2) in the class, add the reference to the sampler:
import org.apache.jmeter.protocol.java.sampler.JUnitSampler;
3) Add the code in the #Test tag to handle variables:
Click to see Code
4) Define variables in JMeter (both in user define variables and CSV)
Click to see JMeter Config
Hope it helps!
I have a simple RESTful web service and I wish to test the PUT method on a certain resource. I would like to do it in the most simple way using as few additional tools as possible.
For instance, testing the GET method of a resource is the peak of simplicity - just going to the resource URL in the browser. I understand that it is impossible to reach the same level of simplicity when testing a PUT method.
The following two assumptions should ease the task:
The request body is a json string prepared beforehand. Meaning, whatever is the solution to my problem it does not have to compose a json string from the user input - the user input is the final json string.
The REST engine I use (OpenRasta) understands certain URL decorators, which tell it what is the desired HTTP method. Hence I can issue a POST request, which would be treated as a PUT request inside the REST engine. This means, regular html form can be used to test the PUT action.
However, I wish the user to be able to enter the URL of the resource to be PUT to, which makes the task more complicated, but eases the testing.
Thanks to all the good samaritans out there in advance.
P.S.
I have neither PHP nor PERL installed, but I do have python. However, staying within the realm of javascript seems to be the simplest approach, if possible. My OS is Windows, if that matters.
I'd suggest using the Poster add-on for Firefox. You can find it over here.
As well as providing a means to inspect HTTP requests coming from desktop and web applications, Fiddler allows you to create arbitrary HTTP requests (as well as resend ones that were previously sent by an application).
It is browser-agnostic.
I use the RESTClient firefox plugin (you can not use an URL for the message body but at least you can save your request) but also would recommend curl on the command line.
Maybe you should also have a look at this SO question.