23
Company LOGO JUnit Week 8, Session 3 Lecturer : ACB - STQA IE 31315 - Institut Teknologi Del Jl. Sisingamangaraja Sitoluama, Laguboti 22381 Toba – SUMUT http://www.del.ac.id

Introduction To J unit

Embed Size (px)

Citation preview

Page 1: Introduction To J unit

Company

LOGO

JUnit Week 8, Session 3

Lecturer : ACB- STQA IE 31315 -

Institut Teknologi Del Jl. SisingamangarajaSitoluama, Laguboti 22381Toba – SUMUThttp://www.del.ac.id

Page 2: Introduction To J unit

04/11/2023 2

Topics

What is JUnit? Why JUnit framework? JUnit mechanics Fixtures Test suites Test runners JUnit classes

STQA/Week09/JUnit

Page 3: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 3

What is JUnit?

An unit-testing tool for Java programs Supports Test-Driven-Development Agile Method Goal: Accelerate programming and

increase the quality of code. Not for testing GUI

Page 4: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 4

JUnit (cont’d)

Each method under test is treated as a unit

The automated testing scripts are in the form of methods in another Java source file The job of the software

developers/testers is then to write the automated testing scripts

Page 5: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 5

Why Junit?

Without JUnit, you will have to use println() to print out some result:

Page 6: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 6

Why JUnit (cont’d)

Without JUnit:

No explicit concept of test passing or failure

No mechanism to collect results in structured fashion

No replicability

Page 7: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 7

Key Goals of JUnit

Easy to use to create tests Create tests that retain their value over

time Leverage existing tests to write new

ones (reusable)

Page 8: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 8

What does JUnit Provide?

API for easily creating Java test cases

Page 9: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 9

What does JUnit Provide?

Comprehensive assertion facilities verify expected versus actual results

Page 10: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 10

What does JUnit Provide?

Test runners for running tests

Page 11: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 11

What does JUnit Provide?

Reporting

Page 12: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 12

What does JUnit Provide?

Aggregation facility (test suites) Used to collect all the test cases Suites can contain testCases and

testSuites

– TestSuite(java.lang.Class theClass,

<java.lang.String name>)

– addTest(Test test) or

addTestSuite(java.lang.Class testClass) Suites can have hierarchy

Page 13: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 13

How to write JUnit-basedtesting code (Minimum)

Include JUnit.jar in the classpath Define a subclass of TestCase class Define one or more public testXXX()

methods in the subclass Write assert methods inside the testXXX

methods() Optionally define main() to run the

TestCase in standalone mode

Page 14: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 14

Test methods

Test methods has name pattern: testXXX() XXX reflects the method of the target class

Test methods must have no arguments Test methods are type of void

Page 15: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 15

Very Simple Test

import junit.framework.TestCase;

public class SimpleTest extends TestCase {public SimpleTest(String name) {

super(name);

}

// Test code

public void testSomething() {System.out.println("About to call assertTrue() method...");assertTrue(4 == (2 * 2));

}

// You don't have to have main() method, use Test runner

public static void main(String[] args){

junit.textui.TestRunner.run(SimpleTest.class);

}

}

Page 16: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 16

Assert Statements

JUnit Assertions are methods starting with assert

Determines the success or failure of a test An assert is simply a comparison between an expected value and an actual value Two variants

assertXXX(...) assertXXX(String message, ...)

the message is displayed when the assertXXX() fails

Page 17: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 17

Assert Statements (cont’d)

Asserts that a condition is true

– assertTrue(boolean condition)

– assertTrue(String message, boolean condition)

Asserts that a condition is false

– assertFalse(boolean condition)

– assertFalse(String message, boolean condition)

Page 18: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 18

Assert Statements (cont’d)

Asserts expected.equals(actual) behavior

– assertEquals(expected, actual)

– assertEquals(String message, expected, actual)

Asserts expected == actual behavior

– assertSame(Object expected, Object actual)

– assertSame(String message, Object expected, Object actual)

Page 19: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 19

Assert Statements (cont’d)

Asserts object reference is null

– assertNull(Object obj)

– assertNull(String message, Object obj) Asserts object reference is not null

– assertNotNull(Object obj)

– assertNotNull(String message, Object obj) Forces a failure

– fail()

– fail(String message)

Page 20: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 20

Fixtures

setUp() and tearDown() used to initialize and release common test data

setUp() is run before every test invocation tearDown() is run after every test method

Page 21: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 21

JUnit Classes

Page 22: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 22

References

www.junit.org http://www.javapassion.com/javase/

javajunit.pdf

Page 23: Introduction To J unit

04/11/2023 STQA/Week09/JUnit 23

THANK YOU