Unit testing and junit

  • Published on

  • View

  • Download


  1. 1. INTERNET PROGRAMMING II Yildiz Technical University 2015 mer Takn Unit Testing & Junit
  2. 2. OUTLINE What is Software Testing Types Unit Testing Integration Testing JUnit Mocking
  3. 3. What is Software Testing Part of the software development life cycle
  4. 4. 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
  5. 5. Software Testing Levels Functional Testing Unit Testing Integration Testing Security Testing Acceptance Testing Alpha Testing Beta Testing etc
  6. 6. 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.
  7. 7. Integration Testing Integration testing is Unit Testing ++ Integration Testing is integrated with Databases, File System, Hardware etc.
  8. 8. 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
  9. 9. JUnit Unit & Integration testing framework @ Java
  10. 10. JUnit Unit & Integration testing framework @ Java junitjunit4.12 Can be used on maven
  11. 11. JUnit Requires @Test annotation to be testable method Return type of methods have to be void @Test public void sampleTestMethod() { // }
  12. 12. JUnit Assertions Assert can check value is null empty true false equals to given etc. assertEquals(); assertTrue(); assertFalse(); assertNotNull(); assertNull();
  13. 13. JUnit Assertions Assert can check value is null empty true false equals to given etc. @Test public void testMethod() { Integer multipliedNums = multiply(1,3); Assert.assertEquals(3, multipliedNums ); } public Integer multiply(int first, int next) { return x*y; } Test passed
  14. 14. Mocking Mocking is keep dependencies out of unit tests
  15. 15. Mocking real objects mocked objects
  16. 16. Mockito Mockito is one of Mocking framework which used in Java org.mockitomockito-all1.10.19 Can be used on maven
  17. 17. Mockito @Mock creates mock object @Mock private CategoryDao categoryDao;
  18. 18. Mockito @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock @InjectMocks private CategoryController categoryController;
  19. 19. Mockito when().then() List categories = new ArrayList(); when(categoryDao.findAll()).thenReturn(categories); when a method was called then simulate something


View more >