Download pptx - Unit testing and junit

Transcript

INTERNET PROGRAMMING IIYildiz Technical University 2015

Ömer Taşkın

Unit Testing & Junit

OUTLINE

• What is Software Testing

• Types

• Unit Testing

• Integration Testing

• JUnit

• Mocking

What is Software Testing

Part of the software development life cycle

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

Software Testing Levels

Functional Testing

Unit Testing

Integration Testing

Security Testing

Acceptance Testing

Alpha Testing

Beta Testing

etc…

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.

Integration Testing

Integration testing is Unit Testing ++

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

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

JUnit

Unit & Integration testing framework @ Java

JUnit

Unit & Integration testing framework @ Java

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

</dependency>

Can be used on maven

JUnit

Requires @Test annotation to be testable method

Return type of methods have to be void

@Testpublic void sampleTestMethod() { //

}

JUnit Assertions

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

assertEquals();

assertTrue();

assertFalse();

assertNotNull();

assertNull();

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

Mocking

Mocking is keep dependencies out of unit tests

Mocking

real objects mocked objects

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

Mockito

@Mock creates mock object

@Mockprivate CategoryDao categoryDao;

Mockito

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

@InjectMocksprivate CategoryController categoryController;

Mockito

when().then()

List<Category> categories = new ArrayList();

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

when a method was called then simulate something

LET’S WORK TOGETHER!


Recommended