How do i pass the HttpHeader object to the JUnit test case?
Now i'm writing the test case of my class.I want to pass the HttpHeader object parameter to my test case method to check whether the method is working or not. So any one give me the suggestion to that.
I have googled alot and wasted much time in getting the perfect way.
Thanking you in advance.
Either create a class that implements the interface, or mock it using a framework like Mockito.
Related
I reported this issue https://github.com/dotnet/runtime/issues/48816 and the developer that responded posted code explaining how to manually deserialize the function argument. Asp is supposed to have an automated system for this however. So if my function definition looks like:
[Authorize]
[HttpPost]
public async Task<IActionResult> Post(Session session)
{
...
then I am supposed to be able to just use session as a variable from that point without any special deserialization step. This suggests to me that there must be a way to set JsonSerializerOptions(JsonSerializerDefaults.Web) somewhere in a more global way so that it can consume the post data it is getting. Alternatively there might be a different way to serialize the post data differently on the client so that it does not get converted to camel case.
The main question is: what is the correct way to serialize and deserialize data objects for asp.net-core?
To Reproduce
Thanks for Roar S's comment, use [FromBody], it works.
I have a large object which can be updated in a few steps. I'am facing a partial binding problem. My service consumes json and I can not use #InitBinder along with #RequestBody. Cutting this object to a few small ones is not good a solution, because there is a lot of cross-field validations between steps.
Do you have any ideas how to solve this? I'am looking for a clean solution like: registering a specific object mapper for given #RequestMapping or something like that. Thanks for help.
You should be able to use a PATCH HTTP method
Its the preferred method that you would use when you need partial updates, like in your case when you want to update just a few fields of a resource
Spring MVC added a support for it in the version 3.2, so you can do something like
#RequestMapping(value="/patch", method=RequestMethod.PATCH, consumes=MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody String patch(#RequestBody Foo foo) {
return foo.toString();
}
and when sending the request, add only the properties you want to update to your PATCH request, the properties that are null or omited won't be updated
In the lack of better Spring MVC PATCH reference, I'm linking this SO thread as an interesting read Spring MVC PATCH method: partial updates
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!
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'm trying to use AutoBean on the server and client to send and receive json data through AppEngines channel API. I don't want to store this data in the datastore. I already have a Proxy for this object that I use for the RequestFactoryServlet (which underneath just uses AutoBean anyways), so this should be doable. Instead of writing up a new Proxy for the object that exactly duplicates the Proxy for the RequestFactoryServlet, I'd like to just use the proxy that I use for the RequestFactoryServlet. The only problem is that I get an error while compiling that comes from my AutoBeanFactory.
Invoking generator
com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator
[ERROR] The com.wmba.wmbaapp.shared.ObjectProxy parameterization is not simple, but the obj method does not provide a
delegate
So I'm not really sure what to do here. It seems like before I added the client side in, it's able to serialize the object into JSON just fine, but for some reason it doesn't like this. It sounds like it wants a delegate from me, but I can't find anything on this from the internet.
Anyone have any ideas?
Note: I also tried the same thing with EntityProxy (which is the base of the RequestFactory framework from what I read on the AutoBean page, but I get the same error).
The issue is that EntityProxy defines the stableId method which is not a getter (name doesn't start with get). That makes it a not simple bean, for which AutoBeans require a real bean instance to be wrapped in the created AutoBean (the delegate, passed as an argument of the type of the AutoBean –ObjectProxy in your case– to your obj method of the AutoBeanFactory).
In other words, AutoBeans expects your obj method to be of the form:
AutoBean<ObjectProxy> obj(ObjectProxy toWrap);
The simplest solution is to not try to reuse the entity proxy with AutoBeans.
You might be able to make it work though by annotating your AutoBeanFactory with:
#Category(EntityProxyCategory.class)
You might have to add #NoWrap(EntityProxyId.class) too, see http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/web/bindery/requestfactory/vm/InProcessRequestFactory.java
It turned out for me that I had a property setter that had an empty parameter list in my Ojbect interface. It didn't have anything to do with the factory, except for the interface the factory was trying to create a proxy for:
interface Factory {
AutoBeans<MyObject> createObject();
}
interface MyObject {
String getProperty();
void setProperty();
}
A bone-headed mistake but held me up with this precise compiler error. Adding the Category annotation as mentioned in the previous answer identified the faulty property setter.