0% found this document useful (0 votes)
46 views5 pages

CS410J: Advanced Java Programming Testing: - Constant Feedback From The Customer Ensures

how to nunit

Uploaded by

MarvLus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views5 pages

CS410J: Advanced Java Programming Testing: - Constant Feedback From The Customer Ensures

how to nunit

Uploaded by

MarvLus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS410J: Advanced Java Programming Testing

Everybody knows that they should test their code, but


Testing is one of the most, if not the most, why don’t they?
important tasks of software engineering. When
software is well-tested, it is easier to document,
understand, and extend. JUnit provides a flexible • There’s not enough time!
and easy-to-use framework for developing unit
tests for Java programs.
• However, the less you test, the less confidence you
have in your code
Unit Testing Java Programs with JUnit
• How do you know it really works?

• Why test your code?


Break the vicious cycle of untested code!

• Using JUnit • Don’t wait until you think your code is finished to
write tests
• Advanced assertions with Hamcrest
• Write tests as you code

Copyright c 2001-2017 by David M. Whitlock. Permission to make digital or hard • Every new piece of functionality should have a test
copies of part or all of this work for personal or classroom use is granted without (“unit test”)
fee provided that copies are not made or distributed for profit or commercial
advantage and that copies bear this notice and full citation on the first page. To
copy otherwise, to republish, to post on servers, or to redistribute to lists, requires
prior specific permission and/or fee. Request permission to publish from • Run your tests every time you recompile
[email protected]. Last updated May 7, 2017.

1 2

Extreme Programming Extreme Objects

Starting in the mid-1990s, Kent Beck and friends began Extreme Programming works very well when developing
evangelizing the concept of “extreme programming” Object-Oriented software

• Software needs to be developed quickly and needs • Classes are extended to add functionality
to evolve even faster
• New tests are added for the new functionality
• Software engineers should develop together: “Pair
Programming”
• The old tests for the old functionality still work
• Testing code should be developed along with the
code being tested Just like “documenting as you code”, “testing as you
code” has many benefits
• It takes a village to write software: architect,
programmer, manager, and customer • You can feel confident that you code works
– Constant feedback from the customer ensures
that the customer always gets what he wants • You find bugs in your code before someone else
does!
• The software must function, and function correctly, at
all times, even if the functionality is limited • You become a consumer of your own code (Is it easy
to use?)
• Extreme Programming emphasizes simplicity and
elegance in software engineering
• If you run all of your tests every time you build, you
– Get things working, then optimize know that your changes haven’t broken anything

3 4
JUnit Configuring tests with annotations

Kent Beck and Erich Gamma (of Design Patterns fame) JUnit was very successful, but some things in the
developed a unit testing framework for Java programs framework were awkward
called JUnit
• Subclassing TestCase meant that your test couldn’t
http://www.junit.org subclass any other class

• test method naming convention made it difficult to


The junit.framework package contains a simple class locate test methods
hierarchy for developing unit tests:
JUnit 4.0 introduced annotations in the org.junit
Assert package for marking test code
assertTrue(boolean) Test
• @Test for test methods
assertEquals(Object, Object)
run(TestResult)
fail(String) – Can have an expected exception and a timeout
in milliseconds

• @Before for setup methods and @After for tear down


TestCase TestSuite methods
TestCase(String testName) * TestSuite(Class testClass)
setUp() addTest(Test) • @BeforeClass and @AfterClass for class-level setup
tearDown()
addTestSuite(Class testClass) ad tear down methods

• @Ignore for @Test methods that shouldn’t be run


Test code subclasses TestCase and test methods are
public, return void, and have names that begin with • Static import for Assert methods
test

5 6

Example Classes Writing a simple TestCase

To demonstrate writing unit tests, we are going to develop The below class tests that adding a Student increases
some classes for modeling Students that are enrolled in the enrollment by one
a Section of a Course
package edu.pdx.cs410J.junit;
* * *
Course Section Student
int term String id import org.junit.Test;
String department import static org.junit.Assert.assertEquals;
int number int year Map grades
import static org.junit.Assert.fail;
int credits addStudent(Student)
dropStudent(Student) public class SectionTest {
double averageGrade()
int getClassSize() @Test
public void testAddStudent() {
Student student = new Student("123-45-6789");
Course course = new Course("CS", 410, 4);
Section section =
new Section(course, Section.SPRING, 2001);
section.addStudent(student);
assertEquals(1, section.getClassSize());
}
}

The assertEquals method is imported from the Assert


class. If its arguments are not equal, then the test fails.

7 8
Running The Test Case Testing Error Conditions

The org.junit.runner.JUnitCore class runs the tests Making sure that your program fails in a well-understood
in a test class and prints out the result to standard out fashion is very important

package edu.pdx.cs410J.junit; To test that the dropStudent method throws an


IllegalArgumentException when you attempt to drop a
import org.junit.runner.JUnitCore; student that isn’t enrolled:
import org.junit.runner.RunWith;
import org.junit.runners.Suite; @Test(expected = IllegalArgumentException.class)
public void testDropStudentNotEnrolled() {
@RunWith(Suite.class) Student student = new Student("123-45-6789");
@Suite.SuiteClasses({SectionTest.class, Course course = new Course("CS", 410, 4);
CourseTest.class}) Section section =
public class AllTests { new Section(course, Section.SPRING, 2001);
section.dropStudent(student);
public static void main(String[] args) { }
JUnitCore.runClasses(AllTests.class);
}
} The fail method is statically imported from Assert and
causes the test to fail
This class defines a suite of tests that run together
If some other problem occurs while the test is running
(e.g. an uncaught exception), the test fails with an “error”.
$ java edu.pdx.cs410J.junit.AllTests

Remember that examples.jar and junit.jar must be


on the classpath

9 10

The Assert class Your test class

The Assert contains methods for validating that certain Test code is just a regular Java class with methods
conditions are true
• Methods annotated with @Before and @After
• assertEquals: Two entities (objects, ints, etc.) methods are invoked before and after the test is run
should be equal (compares objects using equals()) – Establish/disconnect network resources, etc.

• assertNotNull: A value should not be null • Each of your test methods must be annotated with
@Test
• assertSame: Two object references should be the
same (compare objects using ==) The TestSuite class
• assertTrue: A boolean expression should be true Annotating a class with @SuiteClasses specifies a suite
of multiple test classes
• fail: The test should fail • You’ll also need to annotate the class with
@RunWith(Suite.class) to tell the JUnit runner that
When an assertion evaluates to false, the test fails it is a suite

Each assert method is overloaded to have a String


message associated with it

assertEquals("Wrong number of students",


1, section.getClassSize());

11 12
Test Classes and Packages More readable assertions

Should test classes be in the same package as the code JUnit provides some basic methods for validating the
they are testing? state of your tests (assertions), but the code and the
failure messages can be hard to read
Pros:
assertTrue(myString.contains("Hello"));
• Test code can access package-protected methods
and fields When the above fails, all you get is an “expected true,
but got false” error message
• Easy to associate test code with domain code
The Hamcrest library provides powerful “matchers” that
provide readable assertion statements with detailed and
Cons: specific failure messages

• Don’t want to test code to interfere with domain code http://hamcrest.org/JavaHamcrest

• Don’t want to ship test code

When I write JUnit tests, I often have a separate directory


hierarchy for the test code:

~/tests/edu/pdx/cs410J/...

13 14

Hamcrest assertion statements Examples of Hamcrest assertions

Hamcrest provides an assertThat method that asserts @Test


that some value “matches” a “matcher” public void isNullValue() {
assertThat(null, is(nullValue()));
}
• Each “matcher” has a static factory method
@Test
public void isSameInstance() {
• Matchers are composed to form complex assertions Object o = new Object();
assertThat(o, is(sameInstance(o)));
}
• The is matcher is syntactic sugar that aids
readability @Test
public void strings() {
package edu.pdx.cs410J.junit; String s = "Hamcrest is awesome";
assertThat(s, startsWith("Hamcrest"));
import org.junit.Test; assertThat(s, endsWith("awesome"));
assertThat(s, containsString("is"));
import static org.hamcrest.Matchers.*; assertThat(s, is(not(isEmptyString())));
import static org.hamcrest.MatcherAssert.assertThat; assertThat(s, is(equalToIgnoringCase("HAMCREST IS
}
public class HamcrestMatchersTest {

@Test
public void isEqualTo() {
Integer int1 = new Integer("123");
Integer int2 = new Integer("123");
assertThat(int1, is(equalTo(int2)));
}
15 16
Examples of Hamcrest assertions Summary

@Test Testing is a good thing!


public void everyItemIsNotNull() {
List<String> list = Arrays.asList("a", "b", "c");
assertThat(list, • Improves the quality of your software
everyItem(is(notNullValue(String.class))));
}
• Demonstrates that your software works as specified
@Test
public void numbers() {
double pi = 3.1415; • “Test as you code”
assertThat(pi, is(greaterThan(2.0)));
assertThat(pi, is(both(greaterThan(1.0)).
and(lessThan(4.0)))); • Write your tests first (Test-Driven Development)
assertThat(pi, is(closeTo(3.14, 0.01)));
}

@Test • When you make changes, you can verify that you
public void arrays() { didn’t break anything
Integer[] array = { 1, 2, 3, 4, 5};
assertThat(array, hasItemInArray(4));
assertThat(array, is(arrayWithSize(5))); • Spending time writing tests reducing debugging time
assertThat(array, is(not(emptyArray())));
assertThat(4, isIn(array));
} • JUnit makes it easy to write and run test cases

• Testing will help you get an A in this course!

17 18

You might also like