0% found this document useful (0 votes)
31 views6 pages

Junit5 Mockito

The document explains unit testing in Java applications, specifically using JUnit 5 and Mockito for testing and mocking dependencies. It highlights the importance of unit testing for catching bugs early, improving code quality, and providing confidence in code functionality. Additionally, it introduces JDB as a command-line tool for debugging Java programs in environments without a GUI.

Uploaded by

zizansamiulhaque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Junit5 Mockito

The document explains unit testing in Java applications, specifically using JUnit 5 and Mockito for testing and mocking dependencies. It highlights the importance of unit testing for catching bugs early, improving code quality, and providing confidence in code functionality. Additionally, it introduces JDB as a command-line tool for debugging Java programs in environments without a GUI.

Uploaded by

zizansamiulhaque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Testing and Debugging

JAVA Applications
What is Unit Testing?
 Unit Testing is the process of testing individual units (small pieces) of code — like methods or classes — in
isolation to check if they work as expected.

 In Spring Boot, JUnit 5 is the default testing framework.

 Mockito is used to mock dependencies so we can test only the part of the code we are interested in — without
involving external classes or services

Importance of Unit Testing


 Catch bugs early: Problems are found when the code is still small and easy to
fix.

 Improve code quality: Code becomes easier to maintain and extend.

 Confidence: We know our code works because it has been tested automatically.
JUnit → For writing test cases (allows us to write code that checks if your methods work correctly)
Mockito → For mocking dependencies (a fake version of a class - we can control what it returns)
public class GreetingService {
public String greet(String name) { JUnit Test Without Mockito
return "Hello, " + name;
}
}

public class WelcomeManager {


private GreetingService greetingService = new GreetingService(); // direct dependency

public String welcome(String user) {


return greetingService.greet(user) + "! Welcome!";
}
}
The test creates a WelcomeManager.

@Test It automatically creates and uses a


void testWelcome() { real GreetingService.
WelcomeManager manager = new WelcomeManager();
It runs the method: welcome("Alice")
String result = manager.welcome("Alice");
→ returns "Hello, Alice! Welcome!".
assertEquals("Hello, Alice! Welcome!", result);
} assertEquals(...) checks if the output is
exactly what we expected.
Example 2: Using Mockito to Mock a Dependency
import static org.mockito.Mockito.*;
import static
GreetingService.java WelcomeManager.java org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class WelcomeManager { import org.mockito.Mockito;
public class GreetingService {
private GreetingService greetingService;
public String greet(String name) class WelcomeManagerTest {
{
public WelcomeManager(GreetingService
return "Hello, " + name; @Test
greetingService) {
} void testWelcome() {
this.greetingService = greetingService; GreetingService mockService =
}
} Mockito.mock(GreetingService.class);

public String welcome(String user) { when(mockService.greet("Alice")).thenR


return greetingService.greet(user) + "! eturn("Hello, Alice");
Welcome!";
} WelcomeManager manager = new
WelcomeManager(mockService);
}
String result =
manager.welcome("Alice");

assertEquals("Hello, Alice!
Welcome!", result);
}
Greeting Service is mocked, so we can test }
WelcomeManager in isolation.
Comparing with same Example
// GreetingService.java // WelcomeManager.java (uses constructor injection)
public class GreetingService { public class WelcomeManager {
public String greet(String name) { private GreetingService greetingService;
return "Hello, " + name;
} public WelcomeManager(GreetingService greetingService) {
} this.greetingService = greetingService;
}
// WelcomeManager.java
public class WelcomeManager { public String welcome(String user) {
private GreetingService greetingService; return greetingService.greet(user) + "! Welcome!";
}
public WelcomeManager() { }
this.greetingService = new GreetingService(); // hard-coded
} // WelcomeManagerTest.java (JUnit + Mockito)
import static org.mockito.Mockito.*;
public String welcome(String user) { import static org.junit.jupiter.api.Assertions.*;
return greetingService.greet(user) + "! Welcome!"; import org.junit.jupiter.api.Test;
}
} class WelcomeManagerTest {

// WelcomeManagerTest.java (JUnit only) @Test


import static org.junit.jupiter.api.Assertions.*; void testWelcome() {
import org.junit.jupiter.api.Test; GreetingService mockService = mock(GreetingService.class);
when(mockService.greet("Alice")).thenReturn("Hello, Alice");
class WelcomeManagerTest {
WelcomeManager manager = new WelcomeManager(mockService);
@Test String result = manager.welcome("Alice");
void testWelcome() {
WelcomeManager manager = new WelcomeManager(); assertEquals("Hello, Alice! Welcome!", result);
String result = manager.welcome("Alice"); }
assertEquals("Hello, Alice! Welcome!", result); }
} •We test only WelcomeManager's logic.
} •We can simulate any behavior from GreetingService, even errors.
•More reliable and focused tests.
What is JDB?
JDB is a command-line tool included with the JDK. It allows you to debug Java programs without a graphical user interface (GUI).
It’s useful when working in headless environments like servers or containers.

🔧 Basic Workflow Using JDB


Compile your Java class with javac -g to include debug info.

Use commands like:


When to Use JDB vs IDEs
stop at MyClass:25 → Set breakpoint at line 25
Local Development IDE
run → Start execution Remote/server-side debugging JDB
Low-resource environments JDB
step / next → Step through code

print variableName → View a variable's value

locals → List current method variables

You might also like