Top Banner
INTERNET PROGRAMMING II Yildiz Technical University 2015 Ömer Taşkın Unit Testing & Junit
22

Unit testing and junit

Aug 06, 2015

Download

Education

Ömer Taşkın
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit testing and junit

INTERNET PROGRAMMING IIYildiz Technical University 2015

Ömer Taşkın

Unit Testing & Junit

Page 2: Unit testing and junit

OUTLINE

• What is Software Testing

• Types

• Unit Testing

• Integration Testing

• JUnit

• Mocking

Page 3: Unit testing and junit

What is Software Testing

Part of the software development life cycle

Page 4: Unit testing and junit

What is Software Testing

• Points out the defects and errors that were made during the development phases

• Ensures the Quality of product

• Requires lower maintenance cost

Page 5: Unit testing and junit
Page 6: Unit testing and junit

Software Testing Levels

Functional Testing

Unit Testing

Integration Testing

Security Testing

Acceptance Testing

Alpha Testing

Beta Testing

etc…

Page 7: Unit testing and junit
Page 8: Unit testing and junit

Unit Testing

A unit is the smallest testable part of an application like functions, classes, procedures, interfaces.

Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.

Unit testing should be done before Integration testing.

Unit testing simplifies the debugging process.

Page 9: Unit testing and junit

Integration Testing

Integration testing is Unit Testing ++

Integration Testing is integrated with Databases, File System, Hardware etc.

Page 10: Unit testing and junit

Unit Test versus Integration Test

Unit test verifies the logic of small piece of code

Unit tests shouldn't have dependencies on outside systems

Integration test is done to demonstrate that different pieces of the system work together.

Integration tests cover whole applications

Page 11: Unit testing and junit

JUnit

Unit & Integration testing framework @ Java

Page 12: Unit testing and junit

JUnit

Unit & Integration testing framework @ Java

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>

</dependency>

Can be used on maven

Page 13: Unit testing and junit

JUnit

Requires @Test annotation to be testable method

Return type of methods have to be void

@Testpublic void sampleTestMethod() { //

}

Page 14: Unit testing and junit

JUnit Assertions

Assert can check value is null empty true false equals to given etc.

assertEquals();

assertTrue();

assertFalse();

assertNotNull();

assertNull();

Page 15: Unit testing and junit

JUnit Assertions

Assert can check value is null empty true false equals to given etc.

@Testpublic void testMethod() { Integer multipliedNums = multiply(1,3); Assert.assertEquals(3, multipliedNums );}

public Integer multiply(int first, int next) {

return x*y;}

Test passed

Page 16: Unit testing and junit

Mocking

Mocking is keep dependencies out of unit tests

Page 17: Unit testing and junit

Mocking

real objects mocked objects

Page 18: Unit testing and junit

Mockito

Mockito is one of Mocking framework which used in Java

<dependency><groupId>org.mockito</

groupId><artifactId>mockito-all</

artifactId><version>1.10.19</version>

</dependency>

Can be used on maven

Page 19: Unit testing and junit

Mockito

@Mock creates mock object

@Mockprivate CategoryDao categoryDao;

Page 20: Unit testing and junit

Mockito

@InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock

@InjectMocksprivate CategoryController categoryController;

Page 21: Unit testing and junit

Mockito

when().then()

List<Category> categories = new ArrayList();

when(categoryDao.findAll()).thenReturn(categories);

when a method was called then simulate something

Page 22: Unit testing and junit

LET’S WORK TOGETHER!