public int getId() {
return id;
}
public String getName() {
return name;
}
}
dao:
public interface PersonDao {
Person getPerson(int id);
boolean update(Person person);
}
service
public class PersonService {
private final PersonDao personDao;
public PersonService(PersonDao personDao) {
this.personDao = personDao;
}
public boolean update(int id, String name) {
Person person = personDao.getPerson(id);
if (person == null) {
return false;
}
Person personUpdate = new Person(person.getId(), name);
return personDao.update(personUpdate);
}
}
在test目录写测试代码:
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import com.demo.mockito.bean.Person;
import com.demo.mockito.dao.PersonDao;
import com.demo.mockito.service.PersonService;
/**
-
@ClassName: PersonServiceTest
-
@Description: TODO
-
@author 刘彦青
-
@date 2018年7月31日
*/
public class PersonServiceTest {
private PersonDao mockDao;
private PersonService personService;
@Before
public void setUp() throws Exception {
//模拟PersonDao对象
mockDao = mock(PersonDao.class);
when(mockDao.getPerson(1)).thenReturn(new Person(1, “Person1”));
when(mockDao.update(isA(Person.class))).thenReturn(true);
personService = new PersonService(mockDao);
}
@Test
public void testUpdate() throws Exception {
boolean result = personService.update(1, “new name”);
assertTrue(“must true”, result);
//验证是否执行过一次getPerson(1)
verify(mockDao, times(1)).getPerson(eq(1));
//验证是否执行过一次update
verify(mockDao, times(1)).update(isA(Person.class));
}
@Test
public void testUpdateNotFind() throws Exception {
boolean result = personService.update(2, “new name”);
assertFalse(“must true”, result);
//验证是否执行过一次getPerson(1)
verify(mockDao, times(1)).getPerson(eq(1));
//验证是否执行过一次update
verify(mockDao, never()).update(isA(Person.class));
}
}
Mockito使用示例
===========
模拟对象
// 模拟LinkedList 的一个对象
LinkedList mockedList = mock(LinkedList.class);
// 此时调用get方法,会返回null,因为还没有对方法调用的返回值做模拟
System.out.println(mockedList.get(0));
模拟方法调用的返回值
// 模拟获取第一个元素时,返回字符串first。 给特定的方法调用返回固定值在官方说法中称为stub。
when(mockedList.get(0)).thenReturn(“first”);
// 此时打印输出first
System.out.println(mockedList.get(0));
模拟方法调用抛出异常
// 模拟获取第二个元素时,抛出RuntimeException
when(mockedList.get(1)).thenThrow(new RuntimeException());
// 此时将会抛出RuntimeException
System.out.println(mockedList.get(1));
如果一个函数没有返回值类型,那么可以使用此方法模拟异常抛出
doThrow(new RuntimeException(“clear exception”)).when(mockedList).clear();
mockedList.clear();
模拟调用方法时的参数匹配
// anyInt()匹配任何int参数,这意味着参数为任意值,其返回值均是element
when(mockedList.get(anyInt())).thenReturn(“element”);
// 此时打印是element
System.out.println(mockedList.get(999));
模拟方法调用次数
// 调用add一次
mockedList.add(“once”);
// 下面两个写法验证效果一样,均验证add方法是否被调用了一次
verify(mockedList).add(“once”);
verify(mockedList, times(1)).add(“once”);
校验行为
// mock creation
List mockedList = mock(List.class);
// using mock object
mockedList.add(“one”);
mockedList.clear();
//verification
verify(mockedList).add(“one”);
verify(mockedList).clear();
模拟方法调用(Stubbing)
//You can mock concrete classes, not just interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn(“first”);
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints “first”
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints “null” because get(999) was not stubbed
System.out.println(mockedList.get(999));
verify(mockedList).get(0);
参数匹配
//stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn(“element”);
//stubbing using custom matcher (let’s say isValid() returns your own matcher implementation):
when(mockedList.contains(argThat(isValid()))).thenReturn(“element”);
//following prints “element”
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
//argument matchers can also be written as Java 8 Lambdas
verify(mockedList).add(someString -> someString.length() > 5);
校验方法调用次数
//using mock
mockedList.add(“once”);
mockedList.add(“twice”);
mockedList.add(“twice”);
mockedList.add(“three times”);
mockedList.add(“three times”);
mockedList.add(“three times”);
//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add(“once”);
verify(mockedList, times(1)).add(“once”);
//exact number of invocations verification
verify(mockedList, times(2)).add(“twice”);
verify(mockedList, times(3)).add(“three times”);
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add(“never happened”);
//verification using atLeast()/atMost()
verify(mockedList, atLeastOnce()).add(“three times”);
verify(mockedList, atLeast(2)).add(“five times”);
verify(mockedList, atMost(5)).add(“three times”);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题。
注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题。
注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友
[外链图片转存中…(img-A0JAvveQ-1712671998500)]
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
[外链图片转存中…(img-NX9nj5iq-1712671998500)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!