Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Engineering 1, CS 355 Unit Testing with JUnit

Similar presentations


Presentation on theme: "Software Engineering 1, CS 355 Unit Testing with JUnit"— Presentation transcript:

1 Software Engineering 1, CS 355 Unit Testing with JUnit
UWEC Department of Computer Science Introduction to Object Oriented Programming Software Engineering 1, CS Unit Testing with JUnit CS 245

2 Types of Testing Unit testing – test each unit separately
Integration testing – test whole system with units working together Regression testing – test to make sure additions haven’t broken anything Others…

3 Focus on Unit Testing Want to ensure each Java class that we create works as advertised In past courses, we have primarily tested by creating a “driver” program in the main method of the class Problems with this approach tend not to be complete in testing tend to test at end of coding testing code integrated with class code better to have it separate for design reasons

4 JUnit – A Framework for Unit Testing in Java
JUnit - an open-source project to provide a better solution for unit testing in Java Integrates with many Java IDEs, including Eclipse Central idea: create a separate Java testing unit (usually a class) for each class you’re creating, then provide a means to easily integrate the testing unit with the tested class for unit testing

5 How JUnit Works Assume class Foo Test class must be named FooTest
append string “Test” at end Test class must extend (inherit from) junit.framework.TestCase class e.g. import junit.framework.TestCase; public class FooTest extends TestCase …

6 How JUnit Works (2) Where does the TestCase class come from?
Answer: junit.jar Must add the junit.jar file to your project Java Build Path if not already present

7 How JUnit Works (3) One approach: the test class should contain a test method for each named public method in the original class Note: if multiple constructors, can just have one constructor test method JUnit does not support testing non-public methods Each test method should be named in the format: testOriginalMethod, with first lower case letter capitalized e.g. Foo constructor -> testFoo() e.g. Foo toString -> testToString()

8 How JUnit Works (4) How to execute the test class given that there’s no main method? Execute as a JUnit Test Run As/JUnit Test Most environments, including Eclipse, have JUnit plugins or modules to display the results graphically

9 JUnit Simple Example IsoDate class IsoDateTest class IsoDate.java
IsoDateTest.java

10 JUnit Test Case Methods
Note that JUnit uses “assertions” Assertions are boolean statements that claim that something is true about the code at a particular point Can test for: equality sameness null-ness truth or falsehood

11 JUnit Test Case Methods (2)
assertEquals(primitiveExpected, primitiveActual) assertEquals(objectExpected, objectActual) assertSame(objectExpected, objectActual) assertNotSame(objectExpected, objectActual) assertNull(object) assertNotNull(object) assertTrue(boolean condition) assertFalse(boolean condition) e.g. assertEquals(0, epoch.getTime());

12 JUnit Test Results Two types of problems when running tests
Failures – an assertion failed Failure results displayed Expected and result value displayed in trace Errors – a problem with the Java code Try to make sure you have “solid” code, don’t raise any exceptions/errors

13 Value of Testing In the past, software developers have often put too little energy into testing Testing frameworks like JUnit make testing easier, allow automation, and make it more likely that your code works correctly “When the bar is green, your code is clean.” – saying from the JUnit developers Not always true, but it’s a step in the right direction

14 Test Driven Development
More recent approach Generated from Agile Software Development processes Repeat Develop a test case that meets one requirement (test will of course fail) Write the code to make that test succeed Refactor (redesign) the code to ensure good design Until software is fully developed


Download ppt "Software Engineering 1, CS 355 Unit Testing with JUnit"

Similar presentations


Ads by Google