Sie sind auf Seite 1von 18

Unit testing

What is unit testing ?


Unit testingis a software
development process in which the
smallest testable parts of an
application, called units, are
individually and independently
scrutinized for proper operation.Unit
testingis often automated but it can
also be done manually.
BPMWAVE

Why use unit testing?

Tests Reduce Bugs in New Features


Tests Reduce Bugs in Existing Features
Tests Are Good Documentation
Tests Reduce the Cost of Change
Tests Improve Design
Tests Allow Refactoring
Tests Reduce Fear
Tests Defend Against Other Programmers
Testing Makes Development Faster

BPMWAVE

JUnit 4
JUnit is the most popular java unit testing framework along with TestNG
Advantages and functionalities

adnotations
assertions
test suits
parametrizations
rules
categories
visual reprezentation
works with many additional libraries( ex: Mockito,Arquillian,Selenium )

BPMWAVE

Common annotations
@Test
public void method()

TheTestannotation indicates that the


public void method to which it is attached
can be run as a test case.

@Before
public void method()

TheBeforeannotation indicates that this


method must be executed before each
test in the class, so as to execute some
preconditions necessary for the test.

@BeforeClass
public static void method()

TheBeforeClassannotation indicates that


the static method to which is attached
must be executed once and before all
tests in the class. That happens when the
test methods share computationally
expensive setup (e.g. connect to
database).

@After
public void method()

TheAfterannotation indicates that this


method gets executed after execution of
each test (e.g. reset some variables after
execution of every test, delete temporary
variables etc)

@AfterClass
public static void method()

TheAfterClassannotation can be used


when a method needs to be executed
after executing all the tests in a JUnit Test
Case class so as to clean-up the
expensive set-up (e.g disconnect from a
database). Attention: The method
attached with this annotation (similar
toBeforeClass) must be defined as static.

@Ignore
public static void method()

TheIgnoreannotation can be used when


you want temporarily disable the
execution of a specific test. Every method
BPMWAVEthat is annotated with@Ignorewont be
executed.

Assertions
Assertion

Description

void assertEquals([String message],


expected value, actual value)

Asserts that two values are equal. Values


might be type of int, short, long, byte, char
or java.lang.Object. The first argument is an
optional String message.

void assertTrue([String message], boolean


condition)

Asserts that a condition is true.

void assertFalse([String message],boolean


condition)

Asserts that a condition is false.

void assertNotNull([String message],


java.lang.Object object)

Asserts that an object is not null.

void assertNull([String message],


java.lang.Object object)

Asserts that an object is null.

void assertSame([String message],


java.lang.Object expected, java.lang.Object
actual)

Asserts that the two objects refer to the


same object.

void assertNotSame([String message],


java.lang.Object unexpected,
java.lang.Object actual)

Asserts that the two objects do not refer to


the same object.

void assertArrayEquals([String message],


expectedArray, resultArray)

Asserts that the array expected and the


resulted array are equal. The type of Array
might be int, long, short, char, byte

BPMWAVE

Test suits
Insoftwaredevelopment,atestsuite,lesscommonlyknownasavalidation suite,
isacollectionoftestcasesthatareintendedtobeusedtotestasoftware
programtoshowthatithassomespecifiedsetofbehaviours.Atestsuiteoften
containsdetailedinstructionsorgoalsforeachcollectionoftestcasesand
informationonthesystemconfigurationtobeusedduringtesting.Agroupoftest
casesmayalsocontainprerequisitestatesorsteps,anddescriptionsofthe
followingtests.

BPMWAVE

Parameterized JUnit
Parameterizedunittestsareusedtotestthesamecodeunderdifferent
conditions.Thankstoparameterizedunittestswecansetupatestmethodthat
retrievesdatafromsomedatasource.Thisdatasourcecanbeacollectionof
testdataobjects,externalfileormaybeevenadatabase.Thegeneralideaisto
makeiteasytotestdifferentconditionswiththesameunittestmethod,which
willlimitthesourcecodeweneedtowriteandmakesthetestcodemore
robust.Wecancallthesetestsdata-drivenunittests.

BPMWAVE

BPMWAVE

Rules
Rules add special handling around tests, test cases or test suites.
They can do additional validations common for all tests in the class,
concurrently run multiple test instances, set up resources before each
test or test case and tear them down afterwards.
The rule gets complete control over what will done with the test
method, test case or test suite it is applied to. Complete control
means that the rule decides what to do before and after running it
and how to deal with thrown exceptions.

BPMWAVE

BPMWAVE

Categories
Categories are used to organize tests. For example, we can use them
to easily distinguish performance unit test from integration tests or
slow tests from faster tests.

BPMWAVE

The concept of mocking


To put it simply, mocking means creating an imitation of an object
that simulates the behavior of a real object.
Mocking scenario:
You have Class A that has a dependency on Class B and you want to
test a method in Class A that uses a service from Class B without
testing the Class B service. This is why you create a mock of the
Class B service. This helps you to test Class A method in isolation so
you dont have to test dependencies that already work or already
have tests written for them .

BPMWAVE

Mockito example

BPMWAVE

To create a mock of a class you simply call the static method mock,
or you can also use adnotations (@Mock). Using static methods
when and thenReturns you can tell the mocked class how to
behave.
BPMWAVE

Our unit testing


methodology
We use Jenkins to automatically tell maven to build the project and
run tests or not. Tests are made to programmatically simulate http
post to the server using apache http web client.

BPMWAVE

BPMWAVE

Intrebari ?
?

??
?

?
?

?
BPMWAVE

Das könnte Ihnen auch gefallen