Java IoT Authors: Zakia Bouachraoui, Pat Romanski, Elizabeth White, Liz McMillan, Yeshim Deniz

Related Topics: Java IoT

Java IoT: Article

Designing JUnit Test Cases

Effective functional testing

A variation of this case is the expected behavior of XPathEvaluator when the createExpression() method is passed null. In this case, you can expect some error to occur because the expression cannot be created from nothing.

It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.

Many code sections may appear in more than one test case. An experienced object-oriented developer will be tempted to re-factor these and create common classes and utility methods. In some cases, that makes a lot of sense - for example, a logging procedure should be a common method available for all test cases. However, be careful not to over-engineer the tests - these Java classes are no more than tests that are meant to validate functional behavior of an application.

The test cases are typically fragile. For example, if a developer changes the location of the input file in the testXPathEvaluation test or if the createExpression method changes its signature, the test scripts will fail. Significant rework and change for each test case implementation is inevitable as an application evolves. Thus, traceability is crucial for all test cases. If something fails later, it's important to be able to point the developer to the corresponding test case specification and use case specification to promote speedy bug resolution. Therefore, test cases should be annotated with references to the original specification documents. This could be just a simple code comment or a complex mechanism where each test annotates references to the use cases and features that it tests, and a test failure causes a notification to be sent to the responsible developer. The important thing is to have the reference in the code and to maintain the traceability.

Developing Runtime Event Diagrams
To understand what the test covers, you need to understand how the code being tested functions, and how the various static classes work together to form the dynamic object graph that defines the program's state during the test.

There are many ways to model such behavior, including Granovetter Diagrams and Object Interaction Diagrams. The basic idea is to pictographically explore the code to understand what runtime parts are involved during the test. These techniques can all be described under the moniker Runtime Event Diagrams because they show what events occur while the program is running, rather than what events the classes could theoretically handle. These diagrams are important for a number of reasons.

First, these diagrams expose the code in a way that can easily be understood at a high level, and consequently provide useful documentation. This documentation is different than the documentation inlined into the code. These diagrams show the runtime behavior of the code, where the operation of the code's actual functions takes place, and where the system is more easily understood; most design patterns and other architectural decisions are much more easily understood in terms of objects and references than classes and fields.

In addition, these diagrams catalog what parts of the code are being exercised by any given test, and determine if that test will be affected by future changes to arbitrary code. When the developer is certain that test A utilizes subsystems B, C, and D, she is also certain that changes to B, C, or D will necessitate testing A again (to ensure backwards compatibility).

A good tactic is to attempt to model the system in as few steps as possible. In general, the actual calls being made are irrelevant; the important aspect is how the system works together to achieve the desired goal. This goal can be achieved by using a simplified modeling system that shows only general interactions between objects, with natural language descriptions of what is occurring in the various interactions.

After a Runtime Event Diagram is drawn, it can be incorporated into the class documentation. Note that a few limitations of the diagrams make them more resilient to class modifications. First, the method names are generally not used because they may change over time. Instead of method names, the more general and more easily understood natural language descriptions are used. Second, the diagrams are mainly about the interaction between the parts of the system. This is a high-level architectural design decision, and is less likely to change over time. Lastly, the diagram is built in terms of types, not specific classes. As long as the general types remain the same - which they are likely to do to retain the protocols used to interact with them - the graphs need not be updated.

Once the diagrams are created, they can be used in a variety of ways. For instance, a diagram can be used to gain a quick overview of how a system works and how it uses its interconnected parts to achieve its goals. This is a sort of simplified UML which describes only the system parts that really matter at a glance: instances, their types, the other instances which they reference, and the work being performed by the group.

These diagrams can also be used to gain insight into the system's complexity and how it could be simplified. To identify ways to simplify the system, look for objects that are used in the system once or twice, and investigate where they might fit better. Also, look for repetitive tasks, and try to encapsulate them into a method or a class.

However, the diagrams' most important use is to facilitate testing. By encapsulating the system's state, the diagrams can help solve problems that are occurring with the system. The diagrams' information about what should be happening can be referenced later when a problem appears. In this case, it is simpler to identify what went wrong because it is simple to compare the program's current state with its expected state. Fixes within small components should not change the overall architecture, and by using the Runtime Event Diagram that already exists, you can ensure that the system does indeed still function as expected. Moreover, by showing the system as it exists and works at present, the Runtime Event Diagram will demonstrate how the system can be modified when a more important component must change. By defining the system's behavior as a whole and how it is expected to function, the Runtime Event Diagram becomes a sort of architectural unit test. When the system changes, the changes can more easily be vetted to ensure that the proper functionality is maintained.

The diagrams should be used in situations when the details often obscure the bigger picture. Their high-level nature can be leveraged as insight into what design patterns are being used, or what AntiPatterns are showing themselves. Many other uses are possible, and when a Runtime Event Diagram, test case specification and use case specification fails to capture the necessary detail, it provides a road map that can be used to jump directly into the code.

Leveraging Functional Tests for Regression Testing
Finally, to boost your ROI on your functional testing efforts, configure a process to run these tests automatically in concert with your automated build process. Such a process not only functionally tests code, but also performs regular regression testing at the same time. Most modern development projects involve building upon a large existing code base. If the code base lacks sufficient tests, the team has no practical way to determine if the modifications broke any existing functionality. As a result, such code is difficult to extend or optimize. In contrast, developers with a regression test suite of comprehensive functional tests can improve and extend code without fear of introducing problems that could go undetected. After all, there is nothing like the pleasure of being able to run a regression suite and know that everything is still working as expected.

More Stories By Nada daVeiga

Nada daVeiga is the Product Manager of Java Solutions at Parasoft, where she has been a senior member of Professional Services team for two years. Nada's background includes development of service-oriented architecture for integration of rich media applications such as Artesia Teams, IBM Content Manager, Stellent Content Server and Virage Video Logger. Nada developed J2EE enterprise applications and specialized in content transport frameworks using XML, JMS, SOAP, and JWSDP technologies. As a presales engineer, Nada worked with clients such as Cisco, Fidelity, HBO and Time Warner. Nada holds a bachelors degree in computer science from the University of California, Los Angeles (UCLA).

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.