联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2018-11-02 10:39

COMP/2013 (Lab 04 - 2018)

1

LAB 4: ADDING UNIT TESTS, WORKING WITH BUILD SCRIPTS

Aims:

Add some extra functionality to the Zoo code

o Add your own classes using the class diagram

Add a collection of unit tests

o Understand how to use JUnit, and to test some common issues

Understand why test suites are important to ensure software quality during future

maintenance

Export and alter Ant build scripts in Eclipse

TESTING THE EMPLOYEES

Download ZooAppCompleteLab4.zip from Moodle or use your own code.

At the end of last week's worksheet, you should have ZooKeeper and Admin classes, which have

their own ways of calculating a Christmas bonus:

For a ZooKeeper, the Christmas bonus equation is (salary x 0.05+100)

For an Admin role, the bonus is (salary x 0.08)

Add a new Junit Test Case to the project, in which you will test some of the employee classes

functionality.

If you can't remember how to do this, follow the short video on Moodle under Week 4.

To start with, add the following tests:

o Check get and set salary is working (use assertEquals() )

Think about equivalence classes – what representative values could you use

Think about boundary conditions which need testing

o Check the bonus calculation is working as expected

Make sure you can run the tests and get suitable results. Ask the lab helpers if you aren't sure.

TESTING THE COMPOUND CLASS

Let's suppose we want to make sure that our animal ArrayList is empty when we first create a

compound.

Write a test to do this check. You may have to add a method to Compound to return the

'animals' ArrayList first. In the test, you may find assertTrue useful.

Now let's modify our code with some animal classes.

Refactor Animal to be abstract, if it is not already (it doesn't make sense to create an

instance of a generic Animal, we will always want to create a specific type (bat, lion, monkey

etc.)

Create some invertebrate classes which inherit from the abstract class Invertebrate and in

turn from Animal, from the diagram below:

COMP/2013 (Lab 04 - 2018)

2

Add some appropriate fields and methods to the classes you just added

o E.g. the abstract Invertebrate class may have a numberOfLegs field and

setters/getters

In the abstract Invertebrate class as all invertebrates might have legs

o E.g. the Spider class may have makeWeb method, for producing spider webs

In the Spider class as only Spiders make webs

NOTE remember you can automatically create getters and setters from a private field by clicking on

the field name, and then pressing CTRL+1

Now you should have some specific animal classes you can create as objects, and store in a

Compound.

Another sensible test would be that when we add animals, they are in fact being stored.

Write a test to check that when you add two Spiders, the compound class is storing two

objects in its animals ArrayList

What other classes of behaviour may we want to test? One may be that we can store a LOT of

animals in a compound, such as with fish.

Write a test to automatically store 10,000,000 instances of a fish (e.g. Sardine) in a

compound and make sure this amount is successfully stored

We chose this number on purpose. Note in the test explorer that this test takes > 1 second to run.

That is really too slow for a unit test. If you have 100 or 1000 tests to run (easily the case with a big

project) you need to keep them as fast as possible. If you have to test it, then fine, but always think

about making your test run as quickly as possible.

This gives as a chance to demonstrate another feature though. Change the @Test tag above

the previous test to look like:

COMP/2013 (Lab 04 - 2018)

3

@Test(timeout=1000)

Now run the tests. You should see it fails because it takes too long to run (>1000ms).

You can test for exceptions being thrown the same way e.g.

@Test(expected= IndexOutOfBoundsException.class)

Will only pass if that exception is thrown in the test.

WHY REGRESSION TESTING MATTERS

Let's make a change to the code which will cause it to break. This should demonstrate exactly why

having a suite of tests is a good idea!

You've been ask to alter the computer system so that when you set the salary of the

employee, the salary is reduced by an amount of cash, representing a pension contribution

So you think you will do this in Employee.setSalary:

o Alter this method to set the salary actually stored to be 90% of the salary passed as a

parameter

Re-run your tests - what happens?

Failing tests when you alter legacy code suggests you have altered something which may break

another component of the system. Perhaps you need a different way of handling this pension

contribution…

ADDING MORE TESTS

Add to your set of tests, thinking about what else it would make sense to test for in this Zoo

example. Use the resources at the end of this worksheet to find and try out some different

assert statements, and some optional tags you can use.

You may have to add more functionality to the Zoo classes first – think about adding and

testing the following.

o Check that employee names start with a capital letter when retrieved using the

getter

o Check a default zoo has the correct number of Compounds (if you remember before

we added a default of 30 into the constructor)

o Add and test some code to store tickets prices for each Zoo

o Implement the Doctor class. Add an emergencyPhoneNumber field and appropriate

getters and setters.

The setter for the number should validate the phone number. Write some

unit tests to ensure the validation takes place. You can do this by writing the

tests first if you like (Test Driven Development!)

The validation rules are:

Number should start with a 0

Number should be 11 digits long

COMP/2013 (Lab 04 - 2018)

4

BUILD SCRIPTS IN ECLIPSE

Let's look at an Ant build file for this ZooApp project. Eclipse can generate one for us:

Right click your Zoo project in the Package Explorer

Select Export…General->Ant Build files

Make sure 'Create target to compile project using Eclipse compiler' is set.

In case your current build file is called "build.xml", it will be overridden, unless you alter the

name of either the old or new build file.

A new build file will appear in the Package Explorer with a list of auto generated test targets.

Explore it in both the XML editor and the Ant view (to open choose Window > Show view... >

Ant; once the window is visible drag and drop the generated XML file into it)

The Ant view should look like this, showing the targets which are written in the XML file.

Right click "build-eclipse-compiler" and choose "Run as > Ant Build". This should build the project

using the Eclipse compiler.

If it doesn't work you may need to tell Ant to use the correct JRE

o Error is Class not found:org.eclipse.jdt.core.JDTCompilerAdapter

Right click .xml file, Run as > Ant Build… > JRE tab > choose "Run in same JRE as workspace"

(first option)

NOTE: If you choose "build-project" afterwards you might get an error again, as this time Ant

requires an external JRE (which is the JDK in our case). Right click .xml file, Run as > Ant Build… > JRE

tab > choose "Separate JRE" (third option) and pick the JDK-x.x.x. If you receive other errors try to

find help on the internet in the first place; then ask a lab helper.

COMP/2013 (Lab 04 - 2018)

5

When it does work, you should see the BUILD SUCCESSFUL text in the console.

Now run your test class targets (named after your test classes - see red boxes in diagram)

Then run junitreport which will produce HTML report files of your test results, which you can

find in the junit folder in your project directory.

Keep in mind this style of reporting tests for next week when we look at server-based tools.

Final challenge:

Write a new test target to compile, test and generate the report files.

RESOURCES

There are many good online Unit Testing tutorials, including:

http://junit.org/

The documentation for Junit, including all the assertion etc. statements.

http://www.vogella.com/tutorials/JUnit/article.html

Good, thorough article on using testing in Eclipse

http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqsjunit.htm

Simple first steps


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp