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

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

  1. [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith

    Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...

  2. [Spring Unit Testing] Spring Unit Testing with a Java Context

    For example, we want to test against a implemataion: package com.example.in28minutes.basic; import o ...

  3. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  4. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  5. [Spring Boot] Introduce to Mockito

    We have the implemetion: @SpringBootApplication public class MockitoDemoApplication { public static ...

  6. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

  7. 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-program ...

  8. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  9. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

随机推荐

  1. Iterator 接口

    首先要从foreach说起,我们都知道对象,数组和对象可以被foreach语法遍历,数字和字符串却不行.其实除了数组和对象之外PHP内部还提供了一个 Iterator 接口,实现了Iterator接口 ...

  2. cf1104d二分+数学

    #include<bits/stdc++.h> using namespace std; bool ask(int x,int y){ printf("? %d %d\n&quo ...

  3. python 全栈开发,Day12(函数的有用信息,带参数的装饰器,多个装饰器装饰一个函数)

    函数的执行时,*打散.函数的定义时,*聚合. from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(* ...

  4. 判断上学和放假的demo

    var today = new Date(); var xq = today.getDay(); var Now = today.getHours(); if (xq >= 1 &&am ...

  5. RabbitMQ(三): exchange 的使用

    1. Exchange(交换机) 生产者只能发送信息到交换机,交换机接收到生产者的信息,然后按照规则把它推送到对列中. 一方面是接收生产者的消息,另一方面是像队列推送消息. 匿名转发 "&q ...

  6. ERP产品销售发货判断库存功能(四十二)

    产品数量的前端(键盘抬起的事件): <td> <input type="text" name="proCount" onkeyup=" ...

  7. mybatis中的查询语句in用法的相关问题

    在开发的时候,mybatisl中使用in的时候会遇到一些问题,如果我们传的参数是String类型,以“,”来进行隔开的,例如:参数是0,1,2字符串,mybatis中的语句如下 <select ...

  8. POJ 1017 Packets【贪心】

    POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6.  这些产品通常 ...

  9. vtiger7新模块的创建和配置

    vtiger出7.0了,以前的那些配置方法已经不管用了 下面是新的 模块创建及一些页面及功能配置的方法 下面介绍三个点 1.新建一个模块 2.实现单图片上传的功能 3.实现页面summary显示的功能 ...

  10. thinkphp注册验证

    在model中新建一个UserModel //覆盖原本的设置 //一次性获得全部验证错误 protected $patchValidate = true; //实现表单项目验证 //通过重写父类属性_ ...