当前位置:网站首页>Mockito easy to use
Mockito easy to use
2021-10-14 04:49:42 【T の weblog】
1. Some about Mockito Information
Nuggets |Mockito Best practices
2. Build a learning environment Mockito Environment
Create a new one SpringBoot project , Then introduce dependency
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
Create classes commonly used by projects
Learn
public class Learn {
private Integer id;
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Learn() {
}
public Learn(Integer id, String title) {
this.id = id;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Learn{" +
"id=" + id +
", title='" + title + '\'' +
'}';
}
}
LearnController
package run.runnable.learn.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import run.runnable.learn.bean.Learn;
import run.runnable.learn.service.LearnService;
@Controller
public class LearnController {
@Autowired
private LearnService learnService;
@ResponseBody
public Learn getById(Integer id){
return learnService.getById(id);
}
}
LearnDao
package run.runnable.learn.dao;
import run.runnable.learn.bean.Learn;
public interface LearnDao {
Learn getById(Integer id);
int save(Learn learn);
int update(Integer id, Learn learn);
}
LearnServiceImpl
package run.runnable.learn.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import run.runnable.learn.bean.Learn;
import run.runnable.learn.dao.LearnDao;
import run.runnable.learn.service.LearnService;
@Service
public class LearnServiceImpl implements LearnService {
@Autowired
private LearnDao learnDao;
@Override
public Learn getById(Integer id) {
if (id==null){
throw new NullPointerException();
}
return learnDao.getById(id);
}
@Override
public int save(Learn learn) {
if (learn==null){
return 0;
}
return learnDao.save(learn);
}
@Override
public int update(Integer id, Learn learn) {
Learn findLearn = learnDao.getById(id);
findLearn.setTitle(learn.getTitle());
return learnDao.update(id,findLearn);
}
}
LearnService
package run.runnable.learn.service;
import run.runnable.learn.bean.Learn;
public interface LearnService {
Learn getById(Integer id);
int save(Learn learn);
int update(Integer id,Learn learn);
}
3. in the light of LearnServiceImpl unit testing
package run.runnable.learn.service.impl;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import run.runnable.learn.bean.Learn;
import run.runnable.learn.dao.LearnDao;
@ExtendWith(MockitoExtension.class)
public class LearnServiceImplTest {
@Mock
private LearnDao learnDao;
@InjectMocks
private LearnServiceImpl learnService;
@Test
void getById(){
Mockito.when(learnDao.getById(Mockito.any())).thenReturn(new Learn(1,"Math"));
Learn learn = learnService.getById(1);
Assert.assertEquals(new Integer(1),learn.getId());
}
@Test
@DisplayName(" Capture exception ")
public void getByIdWithNull(){
Assertions.assertThrows(NullPointerException.class,()->{
learnService.getById(null);
});
}
@Test
void save(){
Mockito.when(learnDao.save(Mockito.any())).thenReturn(1);
int saveCount = learnService.save(new Learn());
Assert.assertEquals(new Integer(1),new Integer(saveCount));
}
@Test
void saveNull(){
int saveCount = learnService.save(null);
Assert.assertEquals(new Integer(0),new Integer(saveCount));
}
@Test
void update(){
Mockito.when(learnDao.update(Mockito.anyInt(),Mockito.any())).thenReturn(1);
Mockito.when(learnDao.getById(Mockito.any())).thenReturn(new Learn(1,"Math"));
int update = learnService.update(1, new Learn(1, "2"));
Assert.assertEquals(new Integer(1),new Integer(update));
}
}
title :Mockito Easy to use
author : MingGH
Address : https://www.runnable.run/articles/2021/09/26/1632661230958.html
版权声明
本文为[T の weblog]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/10/20211002150216293R.html
边栏推荐
- 【微信小程序爬蟲】錶情包小程序圖文視頻教學,從零寫起,保姆教程!!!
- 【 Wechat applet crawler】 emotion Pack applet picture and Text Video Teaching, Write from zero, Nursing tutorial!!!
- Pourquoi personne ne lit un blog?Essayez ces méthodes et outils (Collection recommandée)
- Leetcode 47. Full arrangement II
- Analyse de l'architecture Sonic
- Product power continues to evolve, and the sales volume of Changan Ford increased month on month in September
- More worrying than the safety of power batteries is the shortage of lithium resources
- Jenkins est trop con?Essayez ces alternatives, l'encens voleur!
- Pression artérielle normale (yzoj - 1078)
- Trouver un nombre naturel spécial (yzoj - 1079)
猜你喜欢
-
Nombre de statistiques à 4 chiffres satisfaisant aux conditions (yzoj - 1080)
-
Somme des séries (yzoj - 1081)
-
Jingjing à rendez - vous (yzoj - 1040)
-
Trouver le plus grand diviseur commun des nombres n (yzoj - 1059)
-
Imprimer le triangle numérique (yzoj - 1068)
-
Mercenaires (yzoj - 1089)
-
Calculer la fonction dérivée du polynôme (yzoj - 1090)
-
Nombre indépendant de 7 (yzoj - 1091)
-
Statistiques (yzoj - 1092)
-
Dessiner un rectangle (yzoj - 1093)
随机推荐
- Pièces d'or (yzoj - 1094)
- Réduction proportionnelle (yzoj - 1095)
- Date de réponse (yzoj - 1096)
- Problème de hanota récursif / récursif (Hanoi)
- Stratégie de préparation à la reprise du concours d'informatique
- Pourquoi un concours d'informatique?
- Jiangsu Yancheng Middle School Information Competition Team (yzoi) Team Regulations
- 2021csp resume Students parents memo
- Se faire des amis - répondre aux questions
- Leetcode horloge quotidienne - 1. Somme des deux nombres
- CSC 321 / 621 analysis
- Le premier caractère unique de la chaîne
- Leetcode horloge quotidienne - 61. Liste de chaînes rotatives
- Échange de doigts d'épée 11. Nombre minimum de tableaux rotatifs
- Leetcode beat Daily - 581. Minimum Disordered Continuous Subarray
- Leetcode frappe - 316 tous les jours.
- Leetcode horloge quotidienne - 2. Ajouter deux chiffres
- Leetcode claquement quotidien - 19. Supprimer l'avant - dernier noeud n de la Liste
- Comparaison de l'architecture technique commune de l'OLAP Open Source
- Leetcode horloge quotidienne - 61. Liste de chaînes rotatives
- Auto - culture du programmeur
- Journal de l'examen d'entrée au collège 16 septembre 2021
- Essai ~ 2021 - 07 - 14
- Comprendre l'échelle complète, la table incrémentale et la table zip de l'entrepôt de données en une minute
- Base de données Union, intersection intersect, différence except
- Scala le sait.
- Basic principle and application of CANopen (I) -- overview of can and its high-level protocol
- Adobe Flash CS6 Chinese compact installation
- Qt5 self made serial port assistant (2) basic control settings
- Smart city local applet smart city local standard source code
- WFD is not connected
- Review knowledge points at the end of software testing (Chapter 3 and Chapter 4)
- Questions d'entrevue classiques sur les réseaux informatiques
- Modbus quick start tutorial of cloud protocol on IOT
- Feiling realizes the communication display scheme of bladder ultrasonic volumeter based on i.mx6q-c core board
- [HFSS] Design of a T-junction magic tee with four ports
- How to debug web API program with visual studio
- Download tensorflow using pychar and Anaconda
- Difference between GPS and RTK (Reprint)
- 月薪没过20K的程序员要注意了!(文末送书)