I have set up this simple http request, which simply returns a "hello world" response to my IDE terminal. I have been looking into testing and I am not quite sure how i would test what this method is returning.
Currently i have done my own research into JUnit, but again i am not even sure if this would be the correct tool to use for this problem. I only researched this as it is a Java tool.
public static void newRequest() throws IOException {
URL helloServer = new URL("http://localhost:5050/");
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(helloServer));
HttpResponse rawResponse = request.execute();
String responseString = rawResponse.parseAsString();
logger.debug(responseString);
}
Any guidance on this would be greatly appreciated.
Does the function even need to be tested?
Does the function even need to be tested? Well, that is entirely up to you. Does this function contain code that is critical to your application? If so then yes. If the impact of a bug in this function is minimal then probably not.
Assuming that you want to test this, then:
The method in question is not returning anything void before the function name says this. You will need to look at testing the logic of the function. In this case you need to check that the correct response is received. There are two ways that I can think of to do this:
Modify the code to return the response.
You could change the function to return a String and then return rawResponse.parseAsString(); (which is the same thing you are logging.
Then you can call the function from the test and check the String that is returned.
Get the log message from your logger.
Depending on the logging that you are using, you could get the log message that was written by the function. Assuming log4j then there are some posts on how to do this:
log4j: how to get the last inserted log message?
Personally, I prefer the first option as it is less effort. I would also consider returning the body of the response rather than the raw response.
Related
I'm making an URL shortener with the Javalin framework and have this endpoint set up:
app.routes(()->{
path("",()->{
get("/:id", ctx->{
//do stuff
ctx.redirect("somewhere.com");
});
});
});
Problem is when I need to serve a javascript file to load into my html files. It tries to load from http://localhost:7000/qrcode.min.js but ends up going to the endpoint mentioned above. From what I read in the documentation this is normal behaviour, Javalin first runs the endpoint handler and then (if it doesn't find an endpoint) runs the file handler.
So how can I fix this? should I define a GET request at "/qrcode.min.js"?, I dont think the javalin context handler has a function that lets me return a .js file.
As Matt already suggested in a comment, it would be way cleaner if you'd prefix either path. That way, you could have /r/:id (or /u/:id with "u" for "URL") and the static files would not get in your way, or you could prefix your static files with e.g. /static/, or even just /s/ for brevity, and your shortened URLs would not get in your way.
If you, however, prefer to stick with your current scheme, you can simply filter out JavaScript files (or any other non-id request) in the handler and instead provide the file (however, if you previously had auto-generated ETags, you'd lose caching if you don't want to handle that yourself).
The latter solution would look like so:
app.routes (() -> {
path ("", () -> {
get ("/:id", ctx -> {
String id = ctx.pathParam ("id");
if (id.endsWith (".js")) {
String resourcePath = "your/classpath/resources/folder/" + id;
try {
InputStream resultStream = Thread.currentThread ()
.getContextClassLoader ()
.getResourceAsStream (resourcePath);
if (resultStream == null)
throw new NullPointerException ("Script not found");
ctx.contentType ("application/javascript");
ctx.result (resultStream);
} catch (NullPointerException e) { // script does not exist
e.printStackTrace (); // for development only!
ctx.status (404);
}
return;
}
// do stuff
ctx.redirect ("somewhere.com");
});
});
});
Depending on your preference, you can also handle the resultStream == null case where my code is currently throwing an NPE to be caught by the outer try/catch and omit the try/catch completely.
Setting the Content-Type is essential so that the browser knows that you're actually responding with JavaScript code. Also, I'm typically using Thread.currentThread ().getContextClassLoader () because we'd want the resource to be resolved based upon the current HTTP handler thread, which could, in theory, have a different class path/class loader than the class we're currently in.
Please note that, as stated above, this will not support client-side caching as the handler simply ignores all ETag headers sent with the request* and instead respond with the complete file which, with many requests in a short amount of time and large scripts, will certainly put way more stress on your disks and CPUs.
Thus, I'd actually recommend to prefix the static files route and let Javalin/Jetty handle all the caching and files magic.
* Actually, the header sent by the client is If-None-Match most of the time. The server would respond with an ETag to allow for caching in the browser.
in my project i would want to implement a plugin for JMeter.
So currently I am stuck at sampler - postprocessing step.
#Override
public void postProcessSampler(HTTPSamplerBase sampler, SampleResult result)
{
super.postProcessSampler(sampler, result);
String postData = sampler.getPropertyAsString(HTTPSamplerBase.ARGUMENTS);
// ...
// apply some operations to postData
// ...
//
// try to write it back to sampler : approach1
// sampler.removeProperty(HTTPSamplerBase.ARGUMENTS);
// sampler.addNonEncodedArgument(HTTPSamplerBase.ARGUMENTS, postData, "");
// Fails
}
So at the postprocessing step i would like to change the request body, whcih is usually stored in HTTPSamplerBase.ARGUMENTS property. However, somehow i cannot set anything to this field. Redefining it with another string gives me a class cast error. If I try operating with strings, then i get invocation exception...
So my question is, what is the correct way to change the sampler's post body?
Regards and thank you
Try out HTTPSamplerBase.getArguments() function instead, example code:
sampler.getArguments().removeAllArguments();
sampler.addNonEncodedArgument("foo","bar","");
sampler.setPostBodyRaw(true);
Also be aware that for this form of post-processing you don't even need to come up with a plugin, all can be done via JSR223 PostProcessor and Groovy language. The above code should work just fine
I want to write a unit test for GET method.I have a GET request that returns a json file of when I request path localhost:9090/application/a/b:
{
name: a,
Age:b
}
I want to write unit test using jersey client. I tried writing as such:
Response response = target("a/b").request(MediaType.APPLICATION_JSON).get();
The objective is to retrieve from the GET response and assert the returned json with an expected value:
assertEqual(expected value:{name: a, Age:b},returned json from response)
However, I am not quite sure how I can write this. Can someone guide me on this? I have been searching through many code samples but mostly it is for POST request so I am not sure how this is implemented for GET request.
Edit: I am not required to post anything (I can just grab most code sample for POST) What I am trying to do is just to invoke a call using the path without sending any json document or object and have it returned to me a response in json. After that, I am supposed to grab this json response, and then assert the object with an expected document. The part that I need guidance on is writing the right Response line of code. Also, since my test class has to inherit a jersey client parent, I am expected to use the Response class. I couldnt find any example like this online. That is why I am here to ask.
Thanks in advance
I think I have an interesting question and PERHAPS there is already the answer which is still a secret for me, so I hope to get some helps from expers. :)
So here is the thing:
I work for the test/validation team to test our Java API and basically my job is to follow test plan and write the test code. After writing that for more than two months, I find the codes are really similar. For example:
To test function could return expected result or throw exception correctly, we may need write several .java to run.
1.java set up server connection, connect client and send request, initiate variables with correct values and pass them to the function A, catch the answer and analyse it
2.java set up server connection, connect client and send request, initiate all variables with correct values but one with bad value and pass them to function A, catch the answer and analyse it
3.java set up server connection, connect client and send request, initiate all variables with correct values but two with bad values and pass them to function A, catch the answer and analyse it
so you see in three java test files, the most part of them are the same or similar enough and even copy/paste make the job boring and possible to be wrong.
I wonder whether or not I could define test code corresponding different behavior, then for every test java file, I define a text including the behavior and then a mother class who is in charge of loading the text file and assembling the final test java file according to the text file?
Like this:
Text File:
1) set up server
2) connect client
3) send request
4) initiate variables with correct values
5) initiate variables with correct values but one with bad value
6) initiate variables with correct values but two with bad values
7) catch the result and analyse it
Mother.java
1) load Text file
2) create a son.java
3) find the code corresponding the Text file and write them to son.java
Then the coder open son.java at IDE to check syntax, or import or anything conflict then run it.
Is my idea realizable or not? Is there already something similar?
Any information would be appreciated, thanks a millions in advance!
Honestly, this does not sound like a good use case for code generation. Instead of generating a class for each test case, you should implement a more general testing utility which takes the required input as its data and executes the generic testing code based on this data.
From what you write, this would for example be something like a simple base class for a JUnit test:
abstract class AbstractServerDependantTest {
protected Server server;
protected Client client;
#Before
public void setUp() {
server = new Server();
server.start();
client = new Client();
client.connectTo(server);
}
#After
public void tearDpwm() {
client.disconnect();
server.shutDown();
}
}
Now you can write three test classes which inherit from this AbstractServerDependantTest without copy pasting your code.
given the following controller method where username = bob and emailAddress = bob#bob.com
public static void resetPassword(String username, String emailAddress) {
String url = BASE_URL + "/users/" + username + "/reset_password";
HttpResponse response = WS.url(url).setParameter("email_address", emailAddress).get();
}
Sometimes when I make the call the url endpoing receives:
localhost:8080/api/v1/users/bob/reset_password?email_address=bob%40bob.com
then other times i get:
localhost:8080/api/v1/users/bob/reset_password?email_address=bob%2540bob.com
On the second one the # has been encoded once to %40 then the % was again encoded to %25 so you end up with %2540
If I do nothing more than wait a minute the problem goes away which makes me think it's some sort of caching problem but I can't seem to figure out what it is.
finally been recognized as a bug and has been fixed in a later release
since this does not repeduce all the times i dont know if that will help but did you try to encode the url? for example :
org.apache.commons.httpclient.util.URIUtil.encodeAll(url);
I think what is happening is when you are calling the controller the first time you send the at symbol as an at symbol.
This then gets encoded into the response and the # is converted to %40. I'm guessing when you get this back and you resend it through the browser the % in the %40 gets encoded to %25 making it %2540.
Sorry if the above is confusing, it is difficult to explain.
Simple answer would be to just do a replace on the emailAddress variable of %40 to # before passing it into WS class. There is also a urlDecode() method in the play framework which may do the trick, I've used play before but I haven't used this method.
I suspect some form of URL rewriting. Do you have an Apache Web Server running in front of your server? Maybe some RewriteRule is missing the [NE] flag.