- 浏览: 1527293 次
- 性别:
- 来自: 厦门
博客专栏
-
Spring 3.x企业实...
浏览量:464213
文章分类
最新评论
-
JyeChou:
学习Spring必学的Java基础知识(1)----反射 -
hhzhaoheng:
...
《Spring4.x企业应用开发实战》光盘资料下载 -
renlongnian:
//assertReflectionEquals(user1, ...
单元测试系列之3:测试整合之王Unitils -
骑着蜗牛超F1:
huang_yong 写道我的经验是,只需定义三层:1.ent ...
Spring的事务管理难点剖析(2):应用分层的迷惑 -
wangyudong:
工具地址貌似更新了哦https://github.com/Wi ...
几种常用的REST webservice客户端测试工具
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
org.mockito
Class Mockito
java.lang.Object org.mockito.Matchers org.mockito.Mockito
public class Mockito
Enables mocks creation, verification and stubbing.
Contents
1. Let's verify some behaviour!2. How about some stubbing?
3. Argument matchers
4. Verifying exact number of invocations / at least once / never
5. Stubbing void methods with exceptions
6. Verification in order
7. Making sure interaction(s) never happened on mock
8. Finding redundant invocations
9. Shorthand for mocks creation - @Mock annotation
10. (**New**) Stubbing consecutive calls (iterator-style stubbing)
11. (**Totally New**) Stubbing with callbacks
12. (**Totally New**) doThrow()|doAnswer()|doNothing()|doReturn() family of methods mostly for stubbing voids
13. (**Totally New**) Spying on real objects
Following examples mock List, because everyone knows its interface (methods like add(), get(), clear() will be used).
You probably wouldn't mock List class 'in real'.
1. Let's verify some behaviour!
//Let's import Mockito statically so that the code looks clearer import static org.mockito.Mockito.*; //mock creation List mockedList = mock(List.class); //using mock object mockedList.add("one"); mockedList.clear(); //verification verify(mockedList).add("one"); verify(mockedList).clear();
Once created, mock will remember all invocations. Then you can selectively verify whatever interaction you are interested in.
2. How about some stubbing?
//You can mock concrete classes, not only interfaces LinkedList mockedList = mock(LinkedList.class); //stubbing stub(mockedList.get(0)).toReturn("first"); stub(mockedList.get(1)).toThrow(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)); //Although it is possible to verify a stubbed invocation, usually it's just redundant //If your code cares what get(0) returns then something else breaks (often before even verify() gets executed). //If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here. verify(mockedList).get(0);
- By default, for all methods that return value, mock returns null, an empty collection or appropriate primitive/primitive wrapper value (e.g: 0, false, ... for int/Integer, boolean/Boolean, ...).
- Stubbing can be overridden: for example common stubbing can go to fixture setup but test methods can override it.
- Once stubbed, mocked method will always return stubbed value regardless of how many times it is called.
- Last stubbing is more important - when you stubbed the same method with the same arguments many times.
3. Argument matchers
//stubbing using built-in anyInt() argument matcher stub(mockedList.get(anyInt())).toReturn("element"); //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher): stub(mockedList.contains(argThat(isValid()))).toReturn("element"); //following prints "element" System.out.println(mockedList.get(999)); //you can also verify using argument matcher verify(mockedList).get(anyInt());
Argument matchers allow flexible verification or stubbing. See the whole library of Matchers
including examples of custom argument matchers / hamcrest matchers.
Warning:
If you are using argument matchers, all arguments have to be provided by matchers.
E.g: (example shows verification but the same applies to stubbing):
verify(mock).someMethod(anyInt(), anyString(), eq("third argument")); //above is correct - eq() is also an argument matcher verify(mock).someMethod(anyInt(), anyString(), "third argument"); //above is incorrect - exception will be thrown because third argument is given without argument matcher.
4. Verifying exact number of invocations / at least once / never
//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 atLeastOnce() verify(mockedList, atLeastOnce()).add("three times");
times(1) is the default. Therefore using times(1) explicitly can be omitted.
5. Stubbing void methods with exceptions
doThrow(Throwable)
replaces stubVoid(Object)
because of improved readability and consistency with the family of doAnswer() methods.
See paragraph 12.
6. Verification in order
List firstMock = mock(List.class); List secondMock = mock(List.class); //using mocks firstMock.add("was called first"); secondMock.add("was called second"); //create inOrder object passing any mocks that need to be verified in order InOrder inOrder = inOrder(firstMock, secondMock); //following will make sure that firstMock was called before secondMock inOrder.verify(firstMock).add("was called first"); inOrder.verify(secondMock).add("was called second");Verification in order is flexible - you don't have to verify all interactions one-by-one but only those that you are interested in testing in order.
Also, you can create InOrder object passing only mocks that are relevant for in-order verification.
7. Making sure interaction(s) never happened on mock
//using mocks - only mockOne is interacted mockOne.add("one"); //ordinary verification verify(mockOne).add("one"); //verify that method was never called on a mock verify(mockOne, never()).add("two"); //verify that other mocks were not interacted verifyZeroInteractions(mockTwo, mockThree);
Instead of verifyZeroInteractions() you can call verifyNoMoreInteractions() but the first one is more explicit and can read better.
8. Finding redundant invocations
//using mocks mockedList.add("one"); mockedList.add("two"); verify(mockedList).add("one"); //following verification will fail verifyNoMoreInteractions(mockedList);Some users who did a lot of classic, expect-run-verify mocking tend to use verifyNoMoreInteractions() very often, even in every test method. verifyNoMoreInteractions() is not recommended to use in every test method. verifyNoMoreInteractions() is a handy assertion from the interaction testing toolkit. Use it only when it's relevant. Abusing it leads to overspecified, less maintainable tests. You can find further reading here.
See also never()
- it is more explicit and communicates an intent well.
9. Shorthand for mocks creation - @Mock annotation
- Minimizes repetitive mock creation code.
- Makes the test class more readable.
- Makes the verification error easier to read because field name is used to identify the mock.
public class ArticleManagerTest { @Mock private ArticleCalculator calculator; @Mock private ArticleDatabase database; @Mock private UserProvider userProvider; private ArticleManager manager;Important! This needs to be somewhere in the base class or a test runner:
MockitoAnnotations.initMocks(testClass);Examples how to write a junit test runner are in Mockito test code (mockito/test/org/mockitousage/examples/junitrunner package);
Read more here: MockitoAnnotations
10. (**New**) Stubbing consecutive calls (iterator-style stubbing)
Sometimes we need to stub with different return value/exception for the same method call. Typical use case could be mocking iterators. Original version of Mockito did not have this feature to promote simple mocking. For example, instead of iterators one could useIterable
or simply collections. Those offer natural ways of stubbing (e.g. using real collections). In rare scenarios stubbing consecutive calls could be useful, though:
stub(mock.someMethod("some arg")) .toThrow(new RuntimeException()) .toReturn("foo"); //First call: throws runtime exception: mock.someMethod("some arg"); //Second call: prints "foo" System.out.println(mock.someMethod("some arg")); //Any consecutive call: prints "foo" as well (last stubbing wins). System.out.println(mock.someMethod("some arg"));
11. (**Totally New**) Stubbing with callbacks
Allows stubbing with genericAnswer
interface.
Yet another controversial feature which was not included in Mockito originally. We recommend using simple stubbing with toReturn() or toThrow() only. Those two should be just enough to test/test-drive any decent (clean & simple) code.
stub(mock.someMethod(anyString())).toAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return "called with arguments: " + args; } }); //Following prints "called with arguments: foo" System.out.println(mock.someMethod("foo"));
12. (**Totally New**) doThrow()|doAnswer()|doNothing()|doReturn() family of methods for stubbing voids (mostly)
Stubbing voids requires different approach fromstub(Object)
because the compiler does not like void methods inside brackets...
doThrow(Throwable)
replaces the stubVoid(Object)
method for stubbing voids. The main reason is improved readability and consistency with the family of doAnswer() methods.
Use doThrow() when you want to stub a void method with an exception:
doThrow(new RuntimeException()).when(mockedList).clear(); //following throws RuntimeException: mockedList.clear();Read more about other methods:
13. (**Totally New**) Spying on real objects
You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).Real spies should be used carefully and occasionally, for example when dealing with legacy code.
Spying on real objects is associated with "partial mocking" concept.
List list = new LinkedList(); List spy = spy(list); //optionally, you can stub out some methods: stub(spy.size()).toReturn(100); //using the spy calls real methods spy.add("one"); spy.add("two"); //prints "one" - the first element of a list System.out.println(spy.get(0)); //size() method was stubbed - 100 is printed System.out.println(spy.size()); //optionally, you can verify verify(spy).add("one"); verify(spy).add("two");
Important gotcha on spying real objects!
Sometimes it's impossible to usestub(Object)
for stubbing spies. Example:
List list = new LinkedList(); List spy = spy(list); //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) stub(spy.get(0)).toReturn("foo"); //You have to use doReturn() for stubbing doReturn("foo").when(spy).get(0);
Mockito() |
static VerificationMode |
atLeastOnce() Allows at-least-once verification. |
|
static Stubber |
doAnswer(Answer answer) Use doAnswer() when you want to stub a void method with generic Answer . |
|
static Stubber |
doNothing() Use doNothing() for setting void methods to do nothing. |
|
static Stubber |
doReturn(java.lang.Object toBeReturned) Use doReturn() in those rare occasions when you cannot use stub(Object) . |
|
static Stubber |
doThrow(java.lang.Throwable toBeThrown) Use doThrow() when you want to stub the void method with an exception. |
|
static InOrder |
inOrder(java.lang.Object... mocks) Creates InOrder object that allows verifying mocks in order. |
|
static
|
mock(java.lang.Class<T> classToMock) Creates mock object of given class or interface. |
|
static
|
mock(java.lang.Class<T> classToMock, java.lang.String name) Creates mock with a name. |
|
static VerificationMode |
never() Alias to times(0), see times(int)
|
|
static
|
spy(T object) Creates a spy of the real object. |
|
static
|
stub(T methodCall) Stubs with return value or exception. |
|
static
|
stubVoid(T mock) Deprecated. Use doThrow(Throwable) method for stubbing voids
|
|
static VerificationMode |
times(int wantedNumberOfInvocations) Allows verifying exact number of invocations. |
|
static
|
verify(T mock) Verifies certain behavior happened once |
|
static
|
verify(T mock, VerificationMode mode) Verifies certain behavior happened at least once / exact number of times / never. |
|
static void |
verifyNoMoreInteractions(java.lang.Object... mocks) Throws an AssertionError if any of given mocks has any unverified interaction. |
|
static void |
verifyZeroInteractions(java.lang.Object... mocks) Verifies that no interactions happened on given mocks. |
anyBoolean, anyByte, anyChar, anyCollection, anyDouble, anyFloat, anyInt, anyList, anyLong, anyMap, anyObject, anyShort, anyString, argThat, booleanThat, byteThat, charThat, contains, doubleThat, endsWith, eq, eq, eq, eq, eq, eq, eq,eq, eq, floatThat, intThat, isA, isNotNull, isNull, longThat, matches, notNull, refEq, same, shortThat, startsWith |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Mockito
public Mockito()
mock
public static <T> T mock(java.lang.Class<T> classToMock)
See examples in javadoc for Mockito
class
classToMock
- class or interface to mockmock
public static <T> T mock(java.lang.Class<T> classToMock, java.lang.String name)
Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. In that case we recommend merciless refactoring.
If you use @Mock annotation then you've got naming mocks for free. @Mock uses field name as mock name.
See examples in javadoc for Mockito
class
classToMock
- class or interface to mockspy
public static <T> T spy(T object)
Real spies should be used carefully and occasionally, for example when dealing with legacy code.
Spying on real objects is associated with "partial mocking" concept.
Example:
List list = new LinkedList(); List spy = spy(list); //optionally, you can stub out some methods: stub(spy.size()).toReturn(100); //using the spy calls real methods spy.add("one"); spy.add("two"); //prints "one" - the first element of a list System.out.println(spy.get(0)); //size() method was stubbed - 100 is printed System.out.println(spy.size()); //optionally, you can verify verify(spy).add("one"); verify(spy).add("two");
Important gotcha on spying real objects!
Sometimes it's impossible to usestub(Object)
for stubbing spies. Example:
List list = new LinkedList(); List spy = spy(list); //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) stub(spy.get(0)).toReturn("foo"); //You have to use doReturn() for stubbing doReturn("foo").when(spy).get(0);
See examples in javadoc for Mockito
class
object
- to spy onstub
public static <T> OngoingStubbing<T> stub(T methodCall)
stub(mock.someMethod()).toReturn(10); //you can use flexible argument matchers, e.g: stub(mock.someMethod(anyString())).toReturn(10); //setting exception to be thrown: stub(mock.someMethod("some arg")).toThrow(new RuntimeException()); //you can stub with different behavior for consecutive calls. //Last stubbing (e.g: toReturn("foo")) determines the behavior for further consecutive calls. stub(mock.someMethod("some arg")) .toThrow(new RuntimeException()) .toReturn("foo");For stubbing void methods with throwables see:
stubVoid(T)
Stubbing can be overridden: for example common stubbing can go to fixture setup but test methods can override it.
Once stubbed, mocked method will always return stubbed value regardless of how many times it is called.
Last stubbing is more important - when you stubbed the same method with the same arguments many times.
Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here.
See examples in javadoc for Mockito
class
methodCall
- method callverify
public static <T> T verify(T mock)
Alias to verify(mock, times(1))
E.g:
verify(mock).someMethod("some arg");Above is equivalent to:
verify(mock, times(1)).someMethod("some arg");
Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here.
See examples in javadoc for Mockito
class
mock
- to be verifiedverify
public static <T> T verify(T mock, VerificationMode mode)
verify(mock, times(5)).someMethod("was called five times"); verify(mock, atLeastOnce()).someMethod("was called at least once"); //you can use flexible argument matchers, e.g: verify(mock, atLeastOnce()).someMethod(anyString());times(1) is the default and can be omitted
See examples in javadoc for Mockito
class
mock
- to be verifiedmode
- times(x), atLeastOnce() or never()verifyNoMoreInteractions
public static void verifyNoMoreInteractions(java.lang.Object... mocks)
You can use this method after you verified your mocks - to make sure that nothing else was invoked on your mocks.
See also never()
- it is more explicit and communicates an intent well.
Stubbed invocations (if called) are also treated as interactions.
Some users who did a lot of classic, expect-run-verify mocking tend to use verifyNoMoreInteractions() very often, even in every test method. verifyNoMoreInteractions() is not recommended to use in every test method. verifyNoMoreInteractions() is a handy assertion from the interaction testing toolkit. Use it only when it's relevant. Abusing it leads to overspecified, less maintainable tests. You can find further reading here.
Example:
//interactions mock.doSomething(); mock.doSomethingUnexpected(); //verification verify(mock).doSomething(); //following will fail because 'doSomethingUnexpected()' is unexpected verifyNoMoreInteractions(mock);See examples in javadoc for
Mockito
class
mocks
- to be verifiedverifyZeroInteractions
public static void verifyZeroInteractions(java.lang.Object... mocks)
verifyZeroInteractions(mockOne, mockTwo);Instead of verifyZeroInteractions() you can call verifyNoMoreInteractions() but the first one is more explicit and can read better.
See examples in javadoc for Mockito
class
mocks
- to be verifiedstubVoid
public static <T> VoidMethodStubbable<T> stubVoid(T mock)
doThrow(Throwable)
method for stubbing voids
//Instead of: stubVoid(mock).toThrow(e).on().someVoidMethod(); //Please do: doThrow(e).when(mock).someVoidMethod();doThrow() replaces stubVoid() because of improved readability and consistency with the family of doAnswer() methods.
Originally, stubVoid() was used for stubbing void methods with exceptions. E.g:
stubVoid(mock).toThrow(new RuntimeException()).on().someMethod(); //you can stub with different behavior for consecutive calls. //Last stubbing (e.g. toReturn()) determines the behavior for further consecutive calls. stub(mock) .toThrow(new RuntimeException()) .toReturn() .on().someMethod();See examples in javadoc for
Mockito
class
mock
- to stubdoThrow
public static Stubber doThrow(java.lang.Throwable toBeThrown)
Stubbing voids requires different approach from stub(Object)
because the compiler does not like void methods inside brackets...
Example:
doThrow(new RuntimeException()).when(mock).someVoidMethod();
toBeThrown
- to be thrown when the stubbed method is calleddoAnswer
public static Stubber doAnswer(Answer answer)
Answer
.
Stubbing voids requires different approach from stub(Object)
because the compiler does not like void methods inside brackets...
Example:
doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Mock mock = invocation.getMock(); return null; }}) .when(mock).someMethod();
answer
- to answer when the stubbed method is calleddoNothing
public static Stubber doNothing()
1. Stubbing consecutive calls on a void method:
doNothing(). doThrow(new RuntimeException()) .when(mock).someVoidMethod(); //does nothing the first time: mock.someVoidMethod(); //throws RuntimeException the next time: mock.someVoidMethod();2. When you spy real objects and you want the void method to do nothing:
List list = new LinkedList(); List spy = spy(list); //let's make clear() do nothing doNothing().when(spy).clear(); spy.add("one"); //clear() does nothing, so the list still contains "one" spy.clear();
doReturn
public static Stubber doReturn(java.lang.Object toBeReturned)
stub(Object)
.
Beware that stub(Object)
is always recommended for stubbing because it is argument type-safe and more readable (especially when stubbing consecutive calls).
However, there are occasions when doReturn() comes handy:
1. When spying real objects and calling real methods on a spy brings side effects
List list = new LinkedList(); List spy = spy(list); //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) stub(spy.get(0)).toReturn("foo"); //You have to use doReturn() for stubbing: doReturn("foo").when(spy).get(0);2. Overriding a previous exception-stubbing:
stub(mock.foo()).toThrow(new RuntimeException()); //Impossible: the exception-stubbed foo() method is really called so RuntimeException is thrown. stub(mock.foo()).toReturn("bar"); //You have to use doReturn() for stubbing: doReturn("bar").when(mock).foo();Above scenario shows a tradeoff of Mockito's ellegant syntax. The scenario is very rare, though.
toBeReturned
- to be returned when the stubbed method is calledinOrder
public static InOrder inOrder(java.lang.Object... mocks)
InOrder inOrder = inOrder(firstMock, secondMock); inOrder.verify(firstMock).add("was called first"); inOrder.verify(secondMock).add("was called second");Verification in order is flexible - you don't have to verify all interactions one-by-one but only those that you are interested in testing in order.
Also, you can create InOrder object passing only mocks that are relevant for in-order verification. See examples in javadoc for Mockito
class
mocks
- to be verified in orderatLeastOnce
public static VerificationMode atLeastOnce()
verify(mock, atLeastOnce()).someMethod("some arg");See examples in javadoc for
Mockito
class
times
public static VerificationMode times(int wantedNumberOfInvocations)
verify(mock, times(2)).someMethod("some arg");See examples in javadoc for
Mockito
class
wantedNumberOfInvocations
- wanted number of invocationsnever
public static VerificationMode never()
times(int)
Verifies that interaction did not happen
verify(mock, never()).someMethod();
See examples in javadoc for Mockito
class
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
发表评论
-
几种常用的REST webservice客户端测试工具
2015-07-30 16:11 9719引言 开发了Rest WebService服务后,如果方便快捷 ... -
dbunit经典的NoSuchColumnException解决之道
2013-08-20 14:36 7350抱怨 dbunit这么多人用 ... -
关于Idea testng单元测试乱码的解决
2013-07-02 10:50 14171问题 我是使用intel ... -
HyperSQL 2.0
2012-11-01 15:15 1962HyperSQL 2.0于本月7日发布了。此次发布距HSQLD ... -
追求代码质量: JUnit 4 与 TestNG 的对比
2012-05-05 15:35 2262经过长时间积极的开发之后,JUnit 4.0 于今年年初 ... -
探索 JUnit 4.4 新特性
2012-05-05 14:51 1795摘自:http://www.ibm.com 随着当前 ... -
单元测试系列之5:使用unitils测试Service层
2012-04-14 10:48 18446引述:Spring 的测试框架为我们提供一个强大的测试环境,解 ... -
单元测试系列之4:使用Unitils测试DAO层
2012-04-12 16:32 19683Spring 的测试框架为我们提供一个强大的测试环境,解 ... -
dbunit和poi版本不兼容的问题
2012-04-07 14:59 6961使用unitiles+dbunit 2.4.8,由于我的 ... -
单元测试系列之2:模拟利器Mockito
2012-03-30 11:38 15312引述:程序测试对 ... -
单元测试系列之1:开发测试的那些事儿
2012-03-28 12:52 10014引述:程序测试对保障应用程序正确性而言,其重要性怎 ...
相关推荐
赠送jar包:mockito-core-2.15.0.jar; 赠送原API文档:mockito-core-2.15.0-javadoc.jar; 赠送源代码:mockito-core-2.15.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-2.15.0.pom; 包含翻译后的API文档...
赠送jar包:mockito-core-4.0.0.jar; 赠送原API文档:mockito-core-4.0.0-javadoc.jar; 赠送源代码:mockito-core-4.0.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-4.0.0.pom; 包含翻译后的API文档:...
Mockito 是一个流行的Java单元测试框架,用于模拟对象行为,使得测试更为简洁和可控。JUnit则是最常用的Java单元测试库,它提供了一种结构化的方式来编写和运行测试用例。当我们进行单元测试时,Mockito 和 JUnit ...
赠送jar包:mockito-core-4.0.0.jar; 赠送原API文档:mockito-core-4.0.0-javadoc.jar; 赠送源代码:mockito-core-4.0.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-4.0.0.pom; 包含翻译后的API文档:...
赠送jar包:powermock-api-mockito2-2.0.9.jar; 赠送原API文档:powermock-api-mockito2-2.0.9-javadoc.jar; 赠送源代码:powermock-api-mockito2-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-api-...
在这个压缩包中,包含的是两个关键工具:JUnit 4.12 和 Mockito,它们是Java开发中广泛使用的单元测试框架。 **JUnit** 是一个开源的Java单元测试框架,它的版本4.12提供了许多增强的功能,包括注解(Annotation)...
Mockito 是一个流行的 Java 单元测试框架,用于模拟(mock)对象,使得开发者可以在测试代码中隔离依赖,专注于测试单个组件的行为。TDD(Test-Driven Development,测试驱动开发)是它常被结合使用的一种开发模式,...
### Mockito的重要入门资料 #### 学习目标与背景 在软件开发过程中,单元测试作为确保代码质量的关键步骤之一,其重要性不言而喻。Mockito作为一种流行的单元测试框架,被广泛应用于Java项目的单元测试中。它通过...
赠送jar包:mockito-core-3.8.0.jar; 赠送原API文档:mockito-core-3.8.0-javadoc.jar; 赠送源代码:mockito-core-3.8.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.8.0.pom; 包含翻译后的API文档:...
### Mockito Programming Cookbook 知识点概述 #### 一、Mockito 概览 **Mockito** 是一个开源的 Java 测试框架,它被设计用于帮助开发者创建模拟对象(mock objects),以便于进行单元测试,支持 **Test-Driven ...
赠送jar包:mockito-core-3.8.0.jar; 赠送原API文档:mockito-core-3.8.0-javadoc.jar; 赠送源代码:mockito-core-3.8.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.8.0.pom; 包含翻译后的API文档:...
赠送jar包:mockito-core-3.1.0.jar; 赠送原API文档:mockito-core-3.1.0-javadoc.jar; 赠送源代码:mockito-core-3.1.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.1.0.pom; 包含翻译后的API文档:...
Mockito 是一个流行的Java单元测试框架,用于模拟对象行为,使得开发者可以在测试代码中独立于实际依赖进行工作。Mockito 1.9.0 是该框架的一个较早版本,但仍然包含许多核心功能,适用于那些对最新版本不敏感或者有...
在本文中,我们将深入探讨如何在Spring Boot项目中有效地使用Mockito进行单元测试。Mockito是一个流行的Java单元测试框架,它允许我们创建和配置模拟对象,以便在测试中隔离我们想要验证的代码部分。结合Spring Boot...
赠送jar包:mockito-core-3.9.0.jar; 赠送原API文档:mockito-core-3.9.0-javadoc.jar; 赠送源代码:mockito-core-3.9.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-3.9.0.pom; 包含翻译后的API文档:...
### Mocks, Mockito, and Spock 在软件开发过程中,单元测试是确保代码质量的关键环节之一。为了提高单元测试的有效性和可维护性,开发者经常使用模拟对象(Mocks)来代替真实对象进行测试。本文将详细介绍Mocks的...
内容概要:参考Mockito官方API文档,实践框架每个特性。 适合人群:Mockito入门人员以及想全面熟悉Mockito特性的人员,做到了开箱即用。 能学到什么:“Mockito 4.6.0 + Junit 5”的组合编程。 使用建议:使用前安装...
TestNG和Mockito是两个在单元测试领域常用的工具,它们分别用于测试管理和模拟对象。本篇文章将深入探讨如何在Spring项目中集成TestNG和Mockito进行单元测试。 TestNG是一个强大的测试框架,它扩展了JUnit的功能,...
赠送jar包:mockito-core-2.15.0.jar; 赠送原API文档:mockito-core-2.15.0-javadoc.jar; 赠送源代码:mockito-core-2.15.0-sources.jar; 赠送Maven依赖信息文件:mockito-core-2.15.0.pom; 包含翻译后的API文档...
mockito-all-1.10.19.jar mockito-core-4.5.1.jar mockito-junit-jupiter-4.5.1.jar mockito-junit-jupiter-4.5.1-sources.jar