JUnit Tutorial

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

JUnit2BTutorial

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://www.onlyfullstack.com/what-is-unit-testing/

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://www.onlyfullstack.com/what-is-junit-how-to-setup-junit-in-eclipse/

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://www.onlyfullstack.com/part-3-annotations-used-in-junit/

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()

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://www.onlyfullstack.com/complete-guide-for-hamcrest-matchers/

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://www.onlyfullstack.com/assert-for-exception-in-junit/

Source Code
Download the source code of JUnit tutorial from below git repository :
unit-testing-and-integration-testing-with-spring-boot