This week we will address dealing with JSON, Design For Testing and intro into mocking.

You will be mocking big time by next week.

1. Study materials

1.1. Slides on testable design and intro into mocking

The slide set on testable design.

The JSON part can be picked up using the youtube videos linked below.

The most common thing you do with Data is transport it. The modern approach is to use a programming language agnostic format: JSON, which stand for JavaScript Object Notation.

Although the name suggests it is JavaScript only, the format is so simple to encode and relatively simple to decode that it is currently the most use format, in particular for web services.

In this week’s topic you will once again see that encoding is typically simpler than decoding. The same as shouting is simpler than listening.

Encoding an object can be as simple as turning the object into a String that conforms JSonĀ“s format and syntax, so no library would be required. However, if you have the opportunity to write

simply use the gson library
  gson.toJson(student);

without even knowing the internal structure of the Student-object, or instead have to write:

hand coded json string formatter for student.
 static String toJson(Student student){
        return "{\"snummer\":"+student.getSnummer()
                +",\"firstname\":\""+student.getFirstName()
                +"\",\"lastName\":\""+student.getLastName()
                +"\",\"born\":{"
                + "\"year\":"+student.getBorn().getYear()
                + ",\"month\":"+student.getBorn().getMonthValue()
                + ",\"day\":"+student.getBorn().getDayOfMonth()
                +"\"},"
                + "\"gender\":\""+student.getGender()+"\"}";
    }

your choice to opt for a library is easily made, if not for anything else as to avoid testing and debugging your way through to a properly format json string.

Test Driven means start with the tests.

A brittle test is a test that is too specific, in this case a test that fails with the slightest difference between expected and actual, although actual still matches its requirement. In the case of a JSON document: Using String.equals(Object jstring) is the worst imaginable test of this kind, because:

  • White space outside the quotes is irrelevant.

  • The order of the key-value pairs is NOT significant.

  • The types of quotes, whether single ''' or double '"' does not matter, as long as they match.

  • Interpretation of escape characters influences the result.

Here is where a special assertion library has its role.

The jsonassert library can be used by any json generating frame work or library

2.1. GSon, Google’s JSON processing library.

You can imagine that google moves a lot of data around. As one of the by-products of their software development, GSon has been created.

The guys at Future Studio created a nice and short 5 part tutorial on GSon, and placed it on youtube. Thx Future Studio.

Future Studio Gson tuturial part 1: Getting started
Future Studio Gson tuturial part 2: Mapping of Nested Objects
Future Studio Gson tuturial part 3: Mapping of Arrays and Lists
Future Studio Gson tuturial part 4: Mapping of Nulls
Future Studio Gson tuturial part 5: Change naming of fields

3. Exercises week 8, Object serialisation with GSon.

Start with a json formatter, hand coded. Turn a Student object into a json formatted string.

3.1. Generate test data

In this exercise you will be generating test data for a university app.

Create a program that generates random object of a particular kind of entity. An example would be a student entity with student number last name and first name, date of birth, gender, cohort, email and student class. The students should be generated as a collection with a command line parameter specifying how many unique students you want. Unique means unique in name, first name, gender and birthdate, so we can tell then apart.

The student number starts at 1 and should be incremented on each generated student. Th given student class already ensures that. It is no problem if you loose a number, but it is when you generate duplicate student numbers. Remember that this is typically the primary key.

You start with three initial files, one containing boys-names, one with girls names, and one with last names. Use these to generate random names. The combination of gender and names should obvious.

Make your data generator optionally output json instead of cvs data and optionally pretty print the json.
To this end, have it interpret the command line parameters (args in public static void main(String[] args)).

  • If an argument is numeric, interpret that as the required amount of student objects.

  • If an argument is '-j', produce json instead of csv records

  • If an argument is '-p' produce pretty printed json. -p implies -j.

You will find a maven project in your repository,

The method names in the test class should give enough hint on the methods purpose.
If not, there will be some java doc.

Decode an existing json document to a student object, Student taking multiple subjects

3.2. Have a candy bar.

Well, actually a candy bar vending machine.

It has been an exam exercise a few years back an could be considered an application where business and presentation are (almost) perfectly separated.

Your task in this practical exercise is not so much to solve the exam exercise, although it gives you the change too what an exam exercise would look like. NO, it is to study the design of this application and see how it was made testable, in particular one question:
What is/are the injection points of this design, to minimise coupling, by studying the source code and the given class diagram in the exam candyvending machine documentation.

The documentation is given as is and may contain more then is shown in the remaining but working maven project. Simply ignore the bits that you do not have to address to solve the task given.

Put your answer in a simple text file named answer.adoc or answer.md in the root of the netbeans/maven project.

You can have your virtual bar by starting the application.

candyvendingmachine
Figure 1. have a bar