Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Testing and the fly likes JUnit testing for REST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit testing for REST" Watch "JUnit testing for REST" New topic

JUnit testing for REST

simon tiberius

Joined: Oct 30, 2012
Posts: 29
my question is related to

I'm trying to implement a RESTful java web services, and I have some question:
I'm making an online shopping web services with following spec (this is just the basics):
1. Customer : CustomerID, Name, Address, PhoneNumber
2. Order (by Customer) : OrderID, CustomerID, CreationDate, GrandTotal (from Parts)
3. Part (by Order) : OrderID, PartID, UnitCost, Quantity, LineTotal (Unit * Quantity)

Now, think specifically about these User Stories related to the Orders (not Customers, not Parts):
1. Customer lists orders to know which orders she has placed
2. Customer creates a new order to be fulfilled.
3. Customer views an existing order to get a reminder of what was ordered
4. Customer updates an order (add or delete parts, or changes quantity) when she realizes it is incorrect.
5. Customer can delete an order that is no longer needed (let’s not worry about if the order has been processed now)

I'm quite new with JUnit. I'm already familiar with @Before, @BeforeClass, @Test and have been using JUnit for some time now. Right now I want to test specifically the "placing an order" part. what should I focus my testing on for this part? security? if so, what's the best practice for that? are mock object and stub used here?thanks
simon tiberius

Joined: Oct 30, 2012
Posts: 29
right now I just want to test the correctness of a customer updates his order. how do I do this? mock objects or stubs? so, something like this:
I'm using JSON format because it's easier to desribe the object although the object that will be tested is java object (Order class and Part class). This JSON format represents the state of the Java objects and the changes during order update:

order = {orderid:1, customerid:7, creationdate:'2012/10/10', calculatedamount:125.00
, part:[{orderid:1, partid:5, unitcost:10, quantity:5}, {orderid:1, partid:6, unitcost:25, quantity:3}]

let's say I update the quantity for partid 5 to 7 and quantity for partid 6 to 5

order = {orderid:1, customerid:7, creationdate:'2012/10/10', calculatedamount:195.00
, part:[{orderid:1, partid:5, unitcost:10, quantity:7}, {orderid:1, partid:6, unitcost:25, quantity:5}]

all updated property are bold. what is the technique in JUnit for this? the object is Java object, not JSON, because what's being tested is the method in the service for updating order. so this is not javascript test, but a java method test. thanks
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31680

There isn't a right/wrong answer between mocks and stubs. I generally use mocks first because I want more control.

As for the JSON, you can either assert against the String or the Java object itself.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
I agree. Here's the link:
subject: JUnit testing for REST