0% found this document useful (0 votes)
0 views

summaries (1)

The document summarizes two videos that explain unit testing in Java using JUnit and Mockito. The first video introduces JUnit for automating tests and emphasizes Test-Driven Development, while the second video discusses handling dependencies in testing, demonstrating the use of stubs and Mockito for creating mock objects. Both tools are essential for writing robust and maintainable Java applications.

Uploaded by

Vaibhav Ghawane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

summaries (1)

The document summarizes two videos that explain unit testing in Java using JUnit and Mockito. The first video introduces JUnit for automating tests and emphasizes Test-Driven Development, while the second video discusses handling dependencies in testing, demonstrating the use of stubs and Mockito for creating mock objects. Both tools are essential for writing robust and maintainable Java applications.

Uploaded by

Vaibhav Ghawane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Summary Report

1. Video 1

<p>Okay, let's break down what this video explains about JUnit and Mockito, and I'll sprinkle in
some code snippets to illustrate the concepts.</p>

<p>So, the video starts by introducing JUnit, which is a testing framework for Java. Think of it as a
tool that helps you ensure your code works as expected. Why is this important? Well, when you're
building a Java application, you'll have lots of classes and methods. You need to be sure that each
of these pieces works correctly. You <em>could</em> test everything manually, but that's tedious,
error-prone, and not scalable, especially for large projects. JUnit allows you to automate this
process.</p>

<p>The video emphasizes the concept of "unit testing." A unit is the smallest testable part of your
application, which in Java often means individual methods within your classes. JUnit helps you test
the behavior of these methods.</p>

<p>Here's a basic example of a class and a corresponding JUnit test, as shown in the video:</p>

<p><strong>Calculator.java (The Class to be Tested)</strong></p>

<p><code>java

public class Calculator {

public int add(int i, int j) {

return i + j;

}</code></p>

<p><strong>TestCalculator.java (The JUnit Test)</strong></p>

<p>```java

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import org.junit.Before;</p>

<p>public class TestCalculator {</p>

<pre><code>private Calculator c;

@Before
Summary Report

public void setup() {

c = new Calculator();

@Test

public void testAdd() {

assertEquals(5, c.add(2, 3));

</code></pre>

<p>}

</p>

<p>Let's unpack this:</p>

<ul>

<li><strong><code>Calculator.java</code></strong>: This is a simple class with an


<code>add</code> method.</li>

<li><strong><code>TestCalculator.java</code></strong>: This is the JUnit test class.</li>

<li><strong><code>@Test</code></strong>: This annotation marks the <code>testAdd</code>


method as a test case that JUnit should run.</li>

<li><strong><code>assertEquals(expected, actual)</code></strong>: This is an assertion. It checks


if the <code>actual</code> value (the result of <code>c.add(2, 3)</code>) is equal to the
<code>expected</code> value (5). If they are equal, the test passes; otherwise, it fails.</li>

<li><strong><code>@Before</code></strong>: This annotation marks the <code>setup</code>


method to be executed before each test. This is where you can initialize objects needed for the
tests.</li>

<li><strong><code>import static org.junit.Assert.assertEquals;</code></strong>: This is a static


import, which allows you to use <code>assertEquals</code> directly without having to write
<code>Assert.assertEquals</code> every time.</li>

</ul>

<p>The video also touches upon Test-Driven Development (TDD), which is a development
approach where you write the tests <em>before</em> you write the actual code. The idea is that
Summary Report

your tests will initially fail because the code doesn't exist yet, and then you write the code to make
the tests pass. This is a good practice, but the video doesn't go into too much detail about it.</p>

<p>Now, let's move on to Mockito. The video explains that sometimes your classes depend on other
classes or services. For example, the video introduces the concept of a
<code>CalculatorService</code> interface:</p>

<p><strong>CalculatorService.java (An Interface)</strong></p>

<p><code>java

public interface CalculatorService {

int add(int i, int j);

}</code></p>

<p>The <code>Calculator</code> class might use this service to perform the addition instead of
doing it itself. This could be because the actual addition logic is provided by a cloud service, a
database, or some other external dependency.</p>

<p>The problem is, if you're testing the <code>Calculator</code> class, you don't necessarily want
to test the <code>CalculatorService</code> at the same time. You want to isolate the
<code>Calculator</code> and test it independently. Also, the <code>CalculatorService</code>
might not even be fully implemented yet, or it might be slow or unreliable.</p>

<p>This is where Mockito comes in. Mockito is a mocking framework. It allows you to create "mock"
objects that mimic the behavior of real objects. You can use a mock
<code>CalculatorService</code> to test the <code>Calculator</code> class without actually
connecting to the real service.</p>

<p>Here's a conceptual example of how you might use Mockito (the video doesn't show the code,
but this is the general idea):</p>

<p>```java

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import static org.mockito.Mockito.*;</p>

<p>public class TestCalculatorWithMockito {</p>

<pre><code>@Test

public void testAddWithService() {

// 1. Create a mock CalculatorService


Summary Report

CalculatorService mockService = mock(CalculatorService.class);

// 2. Define the behavior of the mock service

when(mockService.add(2, 3)).thenReturn(5);

// 3. Create a Calculator object and inject the mock service

Calculator calculator = new Calculator();

// Assuming Calculator has a constructor or setter to inject CalculatorService

// calculator.setService(mockService);

// 4. Test the Calculator using the mock service

assertEquals(5, calculator.add(2, 3));

</code></pre>

<p>}

</p>

<p>In this example:</p>

<ul>

<li><strong><code>mock(CalculatorService.class)</code></strong>: This creates a mock object of


the <code>CalculatorService</code> interface.</li>

<li><strong><code>when(mockService.add(2, 3)).thenReturn(5)</code></strong>: This tells the


mock service that when its <code>add</code> method is called with arguments 2 and 3, it should
return 5.</li>

<li>The <code>Calculator</code> class (not shown) would need to be modified to use the
<code>CalculatorService</code>.</li>

<li>The test then proceeds as before, but now it's using the mock service instead of the real
one.</li>

</ul>

<p>The key takeaway is that Mockito allows you to isolate your tests and avoid dependencies on
external services. This makes your tests faster, more reliable, and easier to maintain.</p>
Summary Report

<p>The video emphasizes that it's not going to show <em>how</em> to use Mockito in this video,
but rather <em>why</em> you need it. The next video will cover the "how."</p>

<p>In summary, JUnit is for unit testing your Java code, and Mockito is for creating mock objects to
isolate your tests from external dependencies. They are both essential tools for writing robust and
maintainable Java applications.</p>

2. Video 2

<p>Okay, let's break down this video and create some detailed notes with code snippets. The core
idea here is about unit testing, specifically how to handle dependencies when testing a class. The
video uses a <code>Calculator</code> class that depends on a <code>CalculatorService</code>,
and it walks us through using both stubs and Mockito to isolate the <code>Calculator</code> for
testing.</p>

<p><strong>The Problem: Testing with Dependencies</strong></p>

<p>Imagine you have a <code>Calculator</code> class. Normally, it might just add two numbers.
But in a real application, this might be more complex. It might need to call out to a cloud service, a
database, or another API to perform part of its calculation. This is what the video refers to as a
"complex operation."</p>

<p>Here's the initial <code>Calculator</code> class (simplified):</p>

<p><code>java

public class Calculator {

public int add(int i, int j) {

return i + j;

}</code></p>

<p>Now, let's say our <code>Calculator</code> needs to use a <code>CalculatorService</code> to


perform the addition. The <code>Calculator</code> class now looks like this:</p>

<p>```java

public class Calculator {

private CalculatorService service;</p>

<pre><code>public Calculator(CalculatorService service) {


Summary Report

this.service = service;

public int perform(int i, int j) {

int sum = service.add(i, j);

return sum * 2;

</code></pre>

<p>}

</p>

<p>And here's the <code>CalculatorService</code> interface:</p>

<p><code>java

public interface CalculatorService {

int add(int i, int j);

}</code></p>

<p>The <code>perform</code> method now uses the <code>CalculatorService</code> to get the


sum, and then multiplies it by 2. The challenge is: how do we test the <code>perform</code>
method of <code>Calculator</code> without actually relying on a real
<code>CalculatorService</code>? We don't want our unit tests to be dependent on the availability
or correctness of an external service.</p>

<p><strong>The First Attempt: Using a Stub</strong></p>

<p>The first approach the video demonstrates is using a "stub." A stub is a fake object that mimics
the behavior of the real dependency. Here's how it's done in the video:</p>

<p>```java

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;</p>

<p>public class CalculatorTest {</p>

<pre><code>@Test

public void testPerform() {


Summary Report

CalculatorService stubService = new CalculatorService() {

@Override

public int add(int i, int j) {

return 0; // A fake implementation

};

Calculator calculator = new Calculator(stubService);

int result = calculator.perform(2, 3);

assertEquals(10, result); // This will fail because the stub returns 0

</code></pre>

<p>}

</p>

<p>In this test, we create an anonymous inner class that implements


<code>CalculatorService</code>. The <code>add</code> method simply returns 0, regardless of
the input. This is a very basic stub. The test will fail because the <code>perform</code> method will
calculate <code>0 * 2 = 0</code>, and we're expecting 10.</p>

<p><strong>The Problem with Stubs</strong></p>

<p>While stubs work, they have limitations:</p>

<ul>

<li><strong>Manual Creation:</strong> You have to manually create and maintain these fake
objects.</li>

<li><strong>Limited Flexibility:</strong> If the behavior of the dependency changes, you have to


update the stub.</li>

<li><strong>No Verification:</strong> You can't easily verify if the <code>Calculator</code> actually


called the <code>add</code> method on the stub.</li>

</ul>

<p><strong>Mockito to the Rescue</strong></p>

<p>This is where Mockito comes in. Mockito is a mocking framework that makes it much easier to
Summary Report

create and manage mock objects.</p>

<p><strong>Adding Mockito Dependency</strong></p>

<p>First, you need to add the Mockito dependency to your project. The video mentions using
Maven, so the dependency would look something like this in your <code>pom.xml</code>:</p>

<p><code>xml

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>2.8.9</version> <!-- Or the latest version -->

<scope>test</scope>

</dependency></code>

(Note: The version number might be different, you can find the latest version on Maven Central
Repository).</p>

<p><strong>Using Mockito to Create a Mock</strong></p>

<p>Here's how you can use Mockito to create a mock object of


<code>CalculatorService</code>:</p>

<p>```java

import org.junit.jupiter.api.Test;

import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.mockito.Mockito.when;</p>

<p>public class CalculatorTest {</p>

<pre><code>@Test

public void testPerformWithMockito() {

CalculatorService mockService = Mockito.mock(CalculatorService.class);

when(mockService.add(2, 3)).thenReturn(5);

Calculator calculator = new Calculator(mockService);

int result = calculator.perform(2, 3);


Summary Report

assertEquals(10, result);

</code></pre>

<p>}

</p>

<p>Let's break this down:</p>

<ol>

<li><code>Mockito.mock(CalculatorService.class)</code>: This line creates a mock object of the


<code>CalculatorService</code> interface. It's a "fake" object that we can control.</li>

<li><code>when(mockService.add(2, 3)).thenReturn(5)</code>: This is the key part. We're telling


Mockito that <em>when</em> the <code>add</code> method of the <code>mockService</code> is
called with arguments 2 and 3, it should <em>return</em> 5. This allows us to simulate the behavior
of the service.</li>

<li>The rest of the test is the same as before, but now the <code>perform</code> method will
calculate <code>5 * 2 = 10</code>, and the test will pass.</li>

</ol>

<p><strong>Mockito's Power: Verification</strong></p>

<p>Mockito also allows us to verify if the mock object's methods were called. This is crucial to
ensure that our code is actually using the dependency as expected.</p>

<p>```java

import org.junit.jupiter.api.Test;

import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.mockito.Mockito.*;</p>

<p>public class CalculatorTest {</p>

<pre><code>@Test

public void testPerformWithMockitoAndVerification() {

CalculatorService mockService = Mockito.mock(CalculatorService.class);

when(mockService.add(2, 3)).thenReturn(5);
Summary Report

Calculator calculator = new Calculator(mockService);

int result = calculator.perform(2, 3);

assertEquals(10, result);

verify(mockService).add(2, 3);

</code></pre>

<p>}

</p>

<p>The <code>verify(mockService).add(2, 3)</code> line checks that the <code>add</code>


method of the <code>mockService</code> was called with the arguments 2 and 3. If the
<code>Calculator</code>'s <code>perform</code> method doesn't call the service, this test will
fail.</p>

<p><strong>Using the <code>@Mock</code> Annotation</strong></p>

<p>Mockito also supports annotations to simplify mock creation. You can use the
<code>@Mock</code> annotation to create mock objects, but you need to enable Mockito's rule for
JUnit:</p>

<p>```java

import org.junit.Rule;

import org.junit.jupiter.api.Test;

import org.mockito.Mock;

import org.mockito.junit.MockitoJUnit;

import org.mockito.junit.MockitoRule;</p>

<p>import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.mockito.Mockito.when;

import static org.mockito.Mockito.verify;</p>

<p>public class CalculatorTest {

@Rule

public MockitoRule rule = MockitoJUnit.rule();</p>


Summary Report

<pre><code>@Mock

private CalculatorService mockService;

@Test

public void testPerformWithMockitoAnnotation() {

when(mockService.add(2, 3)).thenReturn(5);

Calculator calculator = new Calculator(mockService);

int result = calculator.perform(2, 3);

assertEquals(10, result);

verify(mockService).add(2, 3);

</code></pre>

<p>}

</p>

<p>Here:</p>

<ol>

<li><code>@Rule public MockitoRule rule = MockitoJUnit.rule();</code>: This line sets up Mockito's


rule for JUnit, which is needed to use the <code>@Mock</code> annotation.</li>

<li><code>@Mock private CalculatorService mockService;</code>: This line creates a mock object


of <code>CalculatorService</code> and assigns it to the <code>mockService</code> variable.</li>

</ol>

<p><strong>Key Takeaways</strong></p>

<ul>

<li><strong>Unit Testing Isolation:</strong> Mockito helps isolate the class under test by replacing
its dependencies with mock objects.</li>

<li><strong>Controlled Behavior:</strong> You can control the behavior of mock objects, making it
easier to test different scenarios.</li>
Summary Report

<li><strong>Verification:</strong> Mockito allows you to verify if the dependencies were used as


expected.</li>

<li><strong>Simplified Testing:</strong> Mockito makes unit testing with dependencies much easier
and more maintainable than using manual stubs.</li>

</ul>

<p>In essence, Mockito allows you to focus on testing the logic of your class without being bogged
down by the complexities of its dependencies. This leads to more robust and reliable unit tests.</p>

You might also like