writing Test cases for restful service using junit - json

i have been given a project to write test cases for restful service using junit. The restful service provides json data as output.I am new to this, although i have attained basic knowledge but still do not know what to test.Please help.

Ok. So there are several things that could / should be done.
Unit test the Controller directly. Mock all other classes.
Attempt to use any framework provided testing utilities to test how the controller interacts with the framework. An example of this is Spring's MockMVC. For an example, check out the "correct" answer to this question: JUnit test for ExceptionHandler
Both of the above to not require a deployed service to test and so work well in the test phase of maven.
Additionally, although this would be an integration test, you could write JUnit tests using RestTemplate to hit the running service and verify the results. Since this has the external dependancy that the service is deployed and doesn't use any mocks it is an integration test. That said, it is a very valid set of tests and can be written in JUnit. However, these tests should be named (or categorized) as Integration tests so that they can be isolated from the tests that do not have external dependancies.
The fact that the service provides JSon should not be a big deal. Just use Jackson to unmarshal it into a domain object and verify the values are what you expect.

Send request and test the response. You may use restfuse.

Related

Testing CodenameOne business logic using JUnit

I want to write the business logic of my CodenameOne application using JUnit 5. But when I try to run any unit test in Intellij, the simulator is started with the app instead of the unit tests.
Did anybody figure out how to setup Intellij such that the unit tests can be run without triggering the Ant build?
Codename One has its own unit test API and test recorder, generally you should use that for your testing as it allows you to test everything including the GUI.
See this for further details on using the testing framework.
You could probably use JUnit for the pure business logic but I'm not sure what benefit it would provide.

JUnit 5 vs TestNG

We were using JUnit 4, but we had some problems with test grouping. For integration tests we had problems with running order. We had migrated to TestNG in February 2016.
Now we are starting a new independent module for our project and part of our team players wants JUnit 5. I believe it is much better than 4th (there are some new important features, but I see some problems too).
I do not want to use JUnit 5 because of fear of possible problems in our new application, because I have experience with 4th. But I have to be more flexible for the team, so before making a decision I need recommendations from users who have experience with JUnit 5.
TestNG objective is beyond unit test so it covers wider testing needs like Scenario Tests, Integration Test, Dependency Test, Ordering, Parallel Execution etc, but these feature are not supported in JUnit 5.
The JUnit 5 team have these above items in their road map, but they are extending a unit testing framework to support wider range of testing needs. Lets see how the new subsequent release of Junit 5 is able to address the wider testing need in comparison with TestNG
For Spring based application Junit fits best for doing the Unit and Integration Testing as it the default testing framework and Spring Provides lots of Test API with Mockito.
For general testing and Web Automation (Selenium) - TestNG is the de facto.
JUnit 5 introduced a lot of new features. Currently Intellij IDEA supports JUnit 5.
Take a look at article about integrating: Using JUnit 5 in IntelliJ IDEA.
There are some useful annotations now, like:
#Tag Used to declare tags for filtering tests, either at the class
or method level; analogous to test groups in TestNG or Categories in
JUnit 4
#DisplayName Declares a custom display name for the test class or test method
See more: JUnit 5 User Guide

What is the difference between Spring REST service, Jersey REST service and Spring+Jersey solutions?

I want to build a RESTful service/API. I used some framework like play to build it but I want to try other more efficient ways. I heard that Jersey is a common library for building a REST API, and Spring is also a good framework. But I also saw some solutions like Spring+Jersey. Thus, I am a little confused about those REST API solutions.
Can anyone tell me what is the difference among those? Jersey REST, Spring REST and Spring+Jersey REST?
My goal is building a couple of REST APIs that take JSON as input/output. I have jar file as the backend process logic to process the input a JSON/object and return a JSON/object.
Jersey is the JAX-RS API example implementation provided by Sun, while Spring REST is of course Spring's implementation of the same API/JSRs. The major difference is that Spring REST easily integrates into other Spring APIs (if you wish) such as Spring Data Rest.
There are a few noteworthy differences between them - you can "embed" Jersey Resources (known in Spring as Controllers) within each other, to enable a separate class that is responsible for the sub-path of a certain path, while this doesn't appear to be available in Spring right now (you have to define the full path). Also, in my opinion Jersey gives better "out of the box" error responses (such as why it can not map a JSON payload to a Java bean using Jackson) while Spring is a bit more configurable but plainer without some additional work.
In the end the difference in choosing between them usually comes down to - are you already or do you plan to integrate any other Spring libraries into your application? If so Spring REST is the way to go as you'll have a much easier time integrating it, otherwise it is really just personal preference which you'd prefer to use. Personally I like Jersey but the power of other related Spring projects (such as Spring HATEOAS which I highly recommend) makes Spring the better choice. I don't think there will be a real determining factor in your case.
As your "gold" target is a simple API with JSON input/output, I'd recommend you follow the Spring REST guide.
One major difference is in the area of unit testing support.
Jersey Test Framework does not lend itself for mocking server side code - For example, if your REST Resource depended on a Service, you would like to mock the service when testing resource methods. However, Jersey Tests run a separate container and unit tests sort of make calls to the running instance of your REST resource - at this point of time, I have not found any documentation or way for mocking server side code.
On the contrary, Spring MVC tests do not require any containers - and are more well integrated with its controllers. Dependency Injection can be used to inject mock services / DAOs to have better unit tests.
I also find that documentation on Spring projects are more mature when compared to Jersey.
One subtle difference is in the instantiation of the resource (Jersey) or controller (Spring) objects.
Jersey new's a resource object for each request. Whereas, by default Spring treats controllers as beans with default scope of singleton. This can be overridden with a #Scope annotation (although if you do that it will get flagged by Sonar).
This default behavior of Spring has bitten our application several times. With the controller class being a singleton, all class members are effectively static. So values set handling one request will still be there for the next.
This is something to be aware of if your using Spring. My suggestion is to #Scope the controller class as prototype, even though that will earn you a warning if you do Sonar scans.

WSO2 JUnit/Integration Testing

I'm in the process of building a Carbon Archive using the new WSO2 Developer Studio. I'm trying to work out how I can wrap the components (Sequences/Proxies etc) in JUnit tests. These tests will need to run as part of a CI build process (Jenkins) in order to detect errors with any modified code. I've done some research and can't seem to find anything that immediately stands out on how to achieve this. I did find this link https://wso2.org/jira/browse/TOOLS-855
which suggests that it hasn't yet been implemented. Can anyone confirm when this will be implemented or if there is any way at present to achieve this?
There is currently no straight forward way to implement this scenario and this feature will be supported in a future version.
One mechanism i can think is that, add a separate Test module as a part of the build which executes after building C-Apps.
So what happens in here is that, first Jenkins produce the CAR file for C-Apps. Then Maven start executing the JUnit test suite. Before the execution of Test Suite, you can configure maven to copy the CAR files to Servers and start up server. Then execute the Test Cases against the started up server.
This way you can deploy the new CAR files in your Carbon Server and execute the tests against the new configuration in the Server.
Thanks and Regards,
Harshana

Configuring system tests in junit

We currently have a dedicated tool for running system tests on our web services, but I've been thinking of re-writing it to be hostable within jUnit4.
This would give us several advantages, including the full power of Java to set up and assert results, as well as hopefully a simpler method of running the tests (both from CI and the IDE).
However, the tests would need a URL configured for it to test against (it is currently too impractical to initialise a local servlet for testing). Given this, is it still a good idea to try and host it in jUnit? If so, what's the best way to add the configuration?
It depends on how you will be running your unit tests. I normally use maven, then you have a test/resources directory where you can store the test setup.
You can use spring with AbstractDependencyInjectionSpringContextTests (which gives you spring-injected configuration, you can swap the main spring config for a test version)
Here is a dicussion about configuring junit in eclipse
If you're doing non-unit testing, TestNG might be a better option than JUnit. More specifically, data providers seem like they would be a good fit in the scenario you describe (example here). Data providers can retrieve their data pretty much from anywhere: a text/properties/xml/Excel file, a database, a remote host, etc...