[Mockito] Spring Unit Testing with Mockito

It is recommened to write unit testing with Mockito in Spring framework, because it is much faster with Spring framework test. But in case you can doing MVC, you still need to use spring framework test.

In this post, we need to understand why to use Mockito.

For example, we have two class:

package com.example.in28minutes.cdi;

import javax.inject.Named;

@Named
public class SomeCDIDAO {
    public int[] getData() {
        return new int[] {5,89,100};
    }
}

package com.example.in28minutes.cdi;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class SomeCDIBusiness {

    @Inject
    SomeCDIDAO someCDIDAO;

    public SomeCDIDAO getSomeCDIDAO() {
        return someCDIDAO;
    }

    public void setSomeCDIDAO(SomeCDIDAO someCDIDAO) {
        this.someCDIDAO = someCDIDAO;
    }

    public int findGreatest() {
        int greatest = Integer.MIN_VALUE;
        int[] data = someCDIDAO.getData();
        for (int value : data) {
            if (value > greatest) {
                greatest = value;
            }
        }
        return greatest;
    }
}

‘SomeCDIDAO‘ you can consider this class is mainly dealing with Database, fetech data, inserting data. ‘SomeCDIBusiness‘ is using SomeCDIDAO to fetching data.

Whenever, class is dealing with Database, you should always write tests in Mockito. You should mock the opretaion, instead calling the database for real.

package com.example.in28minutes.cdi;

import com.example.in28minutes.In28minutesBasicApplication;
import com.example.in28minutes.In28minutesCdiApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.inject.Inject;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

// Load the context
@RunWith(MockitoJUnitRunner.class)
public class SomeCdiBusinessTest {

    @InjectMocks
    SomeCDIBusiness someCDIBusiness;

    @Mock
    SomeCDIDAO mockDAO;

    @Test
    public void testBasicScenario () {
        Mockito.when(mockDAO.getData()).thenReturn(new int[] {1,2,3});
        int result = someCDIBusiness.findGreatest();
        assertEquals(3, result);
    }

    @Test
    public void testBasicScenario_null () {
        Mockito.when(mockDAO.getData()).thenReturn(new int[] {});
        int result = someCDIBusiness.findGreatest();
        assertEquals(Integer.MIN_VALUE, result);
    }
}


Configuration:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency> -->

原文地址:https://www.cnblogs.com/Answer1215/p/10762751.html

时间: 2024-08-09 11:25:51

[Mockito] Spring Unit Testing with Mockito的相关文章

10 Unit Testing and Automation Tools and Libraries Java Programmers Should Learn

转自:https://javarevisited.blogspot.com/2018/01/10-unit-testing-and-integration-tools-for-java-programmers.html#ixzz60s1lBt5p 一些很不错的测试框架整理 In last a couple of weeks, I have written some articles about what Java developer should learn in 2019 e.g. progr

hadoop编程小技巧(8)---Unit Testing (单元测试)

所需环境: Hadoop相关jar包(下载官网发行版即可): 下载junit包(最新为好): 下载mockito包: 下载mrunit包: 下载powermock-mockito包: 相关包截图如下(相关下载参考:http://download.csdn.net/detail/fansy1990/7690977): 应用场景: 在进行Hadoop的一般MR编程时,需要验证我们的业务逻辑,或者说是验证数据流的时候可以使用此环境,这个环境不要求真实的云平台,只是针对算法或者代码逻辑进行验证,方便调试

Java Unit Testing - JUnit &amp; TestNG

转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignificant programming notes...   |   HOME TABLE OF CONTENTS (SHOW) Java Unit Testing -  & TestNG 1.  Introduction to Unit Testing Framework The various type o

[Unit Testing] AngularJS Unit Testing - Karma

Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. Add test file to the karma.conf.js: // list of files / patterns to load in the browser files: [ 'test/hello.js' ], 2. Add test file: describe('First Un

Unit Testing a zend-mvc application

Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every c

8 Principles of Better Unit Testing

结合工作中的实例,如何设计一个良好的Unit Test,不仅关系到程序的正确性,更关系到有效的缩短整个团队的开发周期(coding, build, refactoring),深刻的关系到敏捷在实际中的应用. 单元测试,是编程契约的一种重要体现.Unit Test应该相信别人会遵守契约.每个Project应该Cover住自己的行为,而不应该去测试别人的行为. Writing good, robust unit tests is not hard -- it just takes a little

Unit Testing PowerShell Code with Pester

Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerShell code. Note   This is a five-part series that includes the following posts: What is Pester and Why Should I Care?Learn about a new test framework

Live Unit Testing

Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是可以在编写代码的时候进行实时的background的单元测试. 在体验之前,有几点注意事项是需要了解的: 1.目前 live unit tesing仅仅支持 C#和VB的传统.net版本,不支持.net core,当然,我觉得也不支持其他的语言,这点是暂时让我遗憾的,因为从体验的结果来看,如果能支持

[Unit Testing] Mock a Node module&#39;s dependencies using Proxyquire

Sometimes when writing a unit test, you know that the module you're testing imports a module that you would like to observe, or at the very least mock to prevent side effects like network activity or file system operations. For JavaScript unit tests