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