[SWM] Day2: Coding tools(Javadocs, Ant, JUnit)

Software Maintenance 2


Coding tools for SWM

There are tools and processes in eclipse which will help you with software maintenance tasks
  • Adding documentation
  • Working with build files(to control your compilation, testing and deployment steps)
  • Building a test suite(for regression testing)

Javadoc comments

Special comments embedded in the source code, which can be used by Eclipse and by a tool to generate HTML documentation
/**....*/ <-note the extra first asterisk
Tags are keywords that do special things, such as @param, @return, @see
Javadoc in Eclipse–> Project->Generate Javadoc

Build files(ANT, Maven, Gradle)

also known as build scripts
a set of instructions for how the project should be ‘compiled’ or built
Eclipse handles some of this for you automatically
But as projects get bigger or more complex, or rely on more external resources, custom build scripts are needed.
They can build in dependencies, package files, run tests, deploy software etc.
Why use build files?
  • Portable
  • Configurable
  • Can script other external processes(testing, metrics, deployment)
  • Ensures consistent compiles and less classpath etc. problems

Ant


  • The first “modern” built tool
  • Apache ant
  • XML
  • Contains one project/ Targets
  • A task is a piece of code that can be executed
  • Reliable, simple set of tools
  • Procedural, but uses XML(not good for huge projects)
What is one project? targets? even properties? and tasks?
I’ll show you a example of build.xml
<?xml version="1.0"?>
<project name="javaTest" default="jar" basedir=".">
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="run" depends="compile">
<java classname="HelloWorld">
<classpath>
<pathelement path="build/classes"/>
</classpath>
</java>
</target>
</project>

Maven

  • Released 2004
  • Also by apache
  • Goal: make a better ant
  • Also XML like ant, but otherwise very different
  • Knows something about where to find sourcefiles etc.
  • In Ant you have to tell it everything
  • Can be more complex than Ant

Gradle

  • Released 2012
  • Tried to take best parts of Ant and Maven
  • Dispenses with XML in favour of a domain specific language
  • Declarative
  • More cleanly accomplishes required tasks of a typical development project, from compilation through testing and deployment.
  • Its the official build system for Android
Using Ant in eclipse: things to watch
  • When you use Ant, it is really running outside of Eclipse
  • If you use the standard Java compiler, Ant will need to be told where the Java JDK is
  • Try right clicking .xml file, Run as->Ant Build…->JRE tab->Separate JRE->Select JDK
Maven :POM file(Program Object Model)

Unit Testing

enter image description here
assertTrue(boolean value) and assertFalse(boolean value)

Think about what to test

we can’t test everything
Equivalence classes(find a representative set of values)
1. If the input value is a range, choose 1 in the range and 2 outside
2. If the input value is a set(e.g. Laptop, Desktop, Phone) then may need separate test if they behave differently, plus one outside the set(e.g.Charger)
3. If a specific condition is required e.g.”Must have a capital letter”, choose one which is positive and one which is negative

Boundary conditions

For example, in a range of numbers, we need to be careful about values just before and just after the range
e.g. If the accepted range is -2.0 to +2.0, test -2.0, -2.01, 2.0, 2.01

Special Values

number 0

Logic(decision coverage)

Develop tests to exercise all logical paths - e.g. both parts of an if-else statement
Examples:
A password string which shouldn’t contain a number
try one contains a number and one doesn’t contains a number
Input is one item specifying the day of the week (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY)
Depends on code: might divided into weekdays and weekends/ might test everyday individually
An age in years, in range 50 to 75
e.g. 49 60 76
enter image description here

Test Driven Development(TDD)

But what’s wrong with writing the Unit tests after the code?
code be “Written to pass?”
Delays testing until the end of a coding session
Refactoring difficult until you have tests
Alternative: TDD:Write the test FIRST!!!!
Test can be derived from business requirements, so it makes sure we meet the spec
Write the minimum amount of code to pass – YAGNI
enter image description here

评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics