Skip to content

Latest commit

 

History

History
132 lines (104 loc) · 5.34 KB

File metadata and controls

132 lines (104 loc) · 5.34 KB

Unit Testing and Mockito Tutorial

In this tutorial, we will understand how to write JUnit test cases. We will understand and implement the below topics.

JUnit Tutorial

Part 1 - What is Unit Testing? In this tutorial, we will understand below topics -

  • What is Unit Testing?
  • Unit Testing Method
  • When is it performed?
  • Who performs it?
  • How to perform it?

https://onlyfullstack.blogspot.com/2019/02/what-is-unit-testing.html

Part 2 - ** What is JUnit? How to use JUnit? In this tutorial, we will understand below topics -

  • What is Junit?
  • How to use Junit?
  • Where should the test be located?
  • How to execute JUnit tests?
    1. Using mvn command
    2. Using Eclipse Run as > JUnit Test
  • Eclipse Optimize Imports to Include Static Imports

https://onlyfullstack.blogspot.com/2019/02/what-is-junit-how-to-use-junit.html

Part 3 - Annotations used in JUnit In this tutorial, we will understand below topics -

  • How to define a test in JUnit?
  • Annotations used in Junit
    1. @Test
    2. @Before
    3. @After
    4. @BeforeClass
    5. @AfterClass
    6. @Ignore or @Ignore("Why disabled")
    7. @Test (expected = Exception.class)

https://onlyfullstack.blogspot.com/2019/02/annotations-used-in-junit.html

Part 4 - JUnit Assert Methods In this tutorial, we will understand below topics -

  • Assert Methods
    1. assertEquals()
    2. assertTrue() + assertFalse()
    3. assertNull() + assertNotNull()
    4. assertSame() and assertNotSame()
    5. assertThat()

https://onlyfullstack.blogspot.com/2019/02/junit-assert-methods.html

Part 5 - Complete guide for Hamcrest JUnit In this tutorial we will understand below topics-

  • Hamcrest Matcher Tutorial
  • The Core Matchers
    1. CoreMatchers.is(T value)
    2. CoreMatchers.not(T value)
    3. CoreMatchers.startsWith(String prefix) & CoreMatchers. endsWith(String suffix)
    4. CoreMatchers.containsString(String substring)
    5. CoreMatchers.notNullValue()
    6. CoreMatchers.sameInstance(T target) & CoreMatchers.instanceOf(Class<?> type)
  • Number Matcher
    1. Matchers.greaterThan(T value) Matchers.greaterThanOrEqualTo(T value)
    2. Matchers.lessThan(T value) Matchers.lessThanOrEqualTo(T value)
  • Collection Matcher
    1. Matchers.empty()
    2. Matchers.hasSize(int size) and Matchers.hasItem(T item)
    3. Matchers. hasKey(K key), Matchers.hasValue(V value) & Matchers.hasEntry(K key, V value)

https://onlyfullstack.blogspot.com/2019/02/complete-guide-for-hamcrest-junit.html

Part 6 - Assert for an exception in JUnit In this tutorial, we will understand below topics-

  • How do you assert that a certain exception is thrown in JUnit 4 tests?
    1. try-catch idiom
    2. @Test expected annotation
    3. Junit @Rule

https://onlyfullstack.blogspot.com/2019/02/assert-for-exception-in-junit.html

_____________________________________________________________________________________________

Mockito Tutorial

Mickito Tutorial

In this tutorial, we will understand how to use Mockito framework. We will understand and implement the below topics.

Part 1 - What is Mockito? In this tutorial, we will understand below topics -

  • What is a Mock Object?
  • When should I mock?
  • Mockito Framework
  • Enable Mockito Annotations
  • How to Mock Object with @Mock & @InjectMock annotations in Spring application?

https://onlyfullstack.blogspot.com/2019/02/what-is-mockito.html

Part 2 - How to mock methods with Mockito?

  • How to mock methods?
    1. when/then
    2. when/thenThrow
    3. when/thenAnswer

https://onlyfullstack.blogspot.com/2019/02/how-to-mock-methods-with-mockito.html

Part 3- How to mock void methods with Mockito

  • Mocking Void Methods
  • Three ways to mock the void method:
    1. doNothing/when
    2. doAnswer/when
    3. doThrow/when

https://onlyfullstack.blogspot.com/2019/02/how-to-mock-void-methods-with-mockito.html

Part 4 - How to verify mocks in Mockito

  • Simple verify method
  • Variations in verify method
  • Verify with the number of times
  • Mockito Verify Order of Invocation

https://onlyfullstack.blogspot.com/2019/02/how-to-verify-mocks-in-mockito.html

Part 5 - **** Mockito Mock vs Spy

  • Mockito Mock vs Spy
    1. Object declaration
    2. When the methods are not mocked
    3. When the method is mocked

https://onlyfullstack.blogspot.com/2019/02/mockito-mock-vs-spy.html