public class MockSettingsImpl extends Object implements MockSettings
Constructor and Description |
---|
MockSettingsImpl() |
Modifier and Type | Method and Description |
---|---|
MockSettings |
defaultAnswer(Answer defaultAnswer)
Specifies default answers to interactions.
|
MockSettings |
extraInterfaces(Class<?>... extraInterfaces)
Specifies extra interfaces the mock should implement.
|
Answer<Object> |
getDefaultAnswer() |
Class<?>[] |
getExtraInterfaces() |
MockName |
getMockName() |
Object |
getSpiedInstance() |
void |
initiateMockName(Class classToMock) |
boolean |
isSerializable() |
MockSettings |
name(String name)
Specifies mock name.
|
MockSettings |
serializable()
Configures the mock to be serializable.
|
MockSettings |
spiedInstance(Object spiedInstance)
Specifies the instance to spy on.
|
public MockSettings serializable()
MockSettings
WARNING: This should be rarely used in unit testing.
The behaviour was implemented for a specific use case of a BDD spec that had an unreliable external dependency. This was in a web environment and the objects from the external dependency were being serialized to pass between layers.
Example:
List serializableMock = mock(List.class, withSettings().serializable());
serializable
in interface MockSettings
public MockSettings extraInterfaces(Class<?>... extraInterfaces)
MockSettings
This mysterious feature should be used very occasionally. The object under test should know exactly its collaborators & dependencies. If you happen to use it often than please make sure you are really producing simple, clean & readable code.
Examples:
Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class)); //now, the mock implements extra interfaces, so following casting is possible: Bar bar = (Bar) foo; Baz baz = (Baz) foo;
extraInterfaces
in interface MockSettings
extraInterfaces
- extra interfaces the should implement.public MockName getMockName()
public Class<?>[] getExtraInterfaces()
public Object getSpiedInstance()
public MockSettings name(String name)
MockSettings
Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. If you have too many mocks then refactor the code so that it's easy to test/debug without necessity of naming mocks.
If you use @Mock annotation then you've got naming mocks for free! @Mock uses field name as mock name. Read more.
Examples:
Foo foo = mock(Foo.class, withSettings().name("foo")); //Below does exactly the same: Foo foo = mock(Foo.class, "foo");
name
in interface MockSettings
name
- the name of the mock, later used in all verification errorspublic MockSettings spiedInstance(Object spiedInstance)
MockSettings
As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.
However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.
Enough warnings about partial mocks, see an example how spiedInstance() works:
Foo foo = mock(Foo.class, spiedInstance(fooInstance)); //Below does exactly the same: Foo foo = spy(fooInstance);
spiedInstance
in interface MockSettings
spiedInstance
- to spy onpublic MockSettings defaultAnswer(Answer defaultAnswer)
MockSettings
It is the default answer so it will be used only when you don't stub the method call.
Foo mock = mock(Foo.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS)); Foo mockTwo = mock(Foo.class, withSettings().defaultAnswer(new YourOwnAnswer())); //Below does exactly the same: Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
defaultAnswer
in interface MockSettings
defaultAnswer
- default answer to be used by mock when not stubbedpublic boolean isSerializable()
public void initiateMockName(Class classToMock)
Copyright © 2018. All rights reserved.