use of org.mockito.MockSettings in project gwt-test-utils by gwt-test-utils.
the class MockitoMockAnnotationProcessor method process.
public Object process(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) {
// never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer().get());
return Mockito.mock(MockAnnotationProcessorHelper.getTypeToMock(field), mockSettings);
}
use of org.mockito.MockSettings in project powermock by powermock.
the class AnnotationEnabler method standardInject.
private void standardInject(Object testInstance) throws IllegalAccessException {
Set<Field> fields = Whitebox.getFieldsAnnotatedWith(testInstance, getMockAnnotations());
for (Field field : fields) {
if (field.get(testInstance) != null) {
continue;
}
final Class<?> type = field.getType();
if (field.isAnnotationPresent(org.powermock.core.classloader.annotations.Mock.class)) {
org.powermock.core.classloader.annotations.Mock annotation = field.getAnnotation(org.powermock.core.classloader.annotations.Mock.class);
final String[] value = annotation.value();
if (value.length != 1 || !"".equals(value[0])) {
System.err.println("PowerMockito deprecation: Use PowerMockito.spy(..) for partial mocking instead. A standard mock will be created instead.");
}
}
if (field.isAnnotationPresent(org.mockito.Mock.class)) {
org.mockito.Mock mockAnnotation = field.getAnnotation(org.mockito.Mock.class);
MockSettings mockSettings = withSettings();
Answers answers = mockAnnotation.answer();
if (answers != null) {
mockSettings.defaultAnswer(answers.get());
}
Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces();
if (extraInterfaces != null && extraInterfaces.length > 0) {
mockSettings.extraInterfaces(extraInterfaces);
}
String name = mockAnnotation.name();
if (name != null && name.length() > 0) {
mockSettings.name(name);
}
field.set(testInstance, mock(type, mockSettings));
} else {
field.set(testInstance, mock(type));
}
}
}
use of org.mockito.MockSettings in project mockito by mockito.
the class MockAnnotationProcessor method process.
public Object process(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) {
// never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
if (annotation.serializable()) {
mockSettings.serializable();
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer());
return Mockito.mock(field.getType(), mockSettings);
}
use of org.mockito.MockSettings in project spring-boot by spring-projects.
the class MockDefinition method createMock.
@SuppressWarnings("unchecked")
public <T> T createMock(String name) {
MockSettings settings = MockReset.withSettings(getReset());
if (StringUtils.hasLength(name)) {
settings.name(name);
}
if (!this.extraInterfaces.isEmpty()) {
settings.extraInterfaces(this.extraInterfaces.toArray(new Class<?>[] {}));
}
settings.defaultAnswer(MockitoApi.get().getAnswer(this.answer));
if (this.serializable) {
settings.serializable();
}
return (T) Mockito.mock(this.typeToMock.resolve(), settings);
}
use of org.mockito.MockSettings in project torodb by torodb.
the class DefaultToBackendFunctionTest method testApply_newDocPart.
@Test
public void testApply_newDocPart() {
MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
});
BatchMetaDocPart allNewDocPart = mock(BatchMetaDocPart.class, settings);
doReturn(true).when(allNewDocPart).isCreatedOnCurrentBatch();
doReturn(Lists.newArrayList(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.BOOLEAN))).stream()).when(allNewDocPart).streamFields();
doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.BOOLEAN)).stream()).when(allNewDocPart).streamScalars();
DocPartData allNewData = mock(DocPartData.class);
given(allNewData.getMetaDocPart()).willReturn(allNewDocPart);
CollectionData collectionData = mock(CollectionData.class);
given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allNewData));
//when
Iterable<BackendTransactionJob> result = fun.apply(collectionData);
ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
//then
assertEquals("Expected 4 jobs to do, but " + resultList.size() + " were recived", 4, resultList.size());
{
Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allNewData)).findAny();
assertTrue(insertJob.isPresent());
Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddFieldDdlJob)) {
return false;
}
AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
}).findAny();
assertTrue(addFieldJob.isPresent());
Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddScalarDddlJob)) {
return false;
}
AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.BOOLEAN);
}).findAny();
assertTrue(addScalarJob.isPresent());
Optional<BackendTransactionJob> createDocPartJob = resultList.stream().filter((job) -> {
if (!(job instanceof AddDocPartDdlJob)) {
return false;
}
AddDocPartDdlJob castedJob = (AddDocPartDdlJob) job;
return castedJob.getDocPart().equals(allNewDocPart);
}).findAny();
assertTrue(createDocPartJob.isPresent());
int createDocPartIndex = resultList.indexOf(createDocPartJob.get());
int addFieldIndex = resultList.indexOf(addFieldJob.get());
int addScalarIndex = resultList.indexOf(addScalarJob.get());
int insertIndex = resultList.indexOf(insertJob.get());
assert createDocPartIndex >= 0;
assert addFieldIndex >= 0;
assert addScalarIndex >= 0;
assert insertIndex >= 0;
assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
assertTrue("For a given doc part, all related add scalar jobs must be executed before insert " + "jobs, but in this case the add scalr job has index " + addScalarIndex + " and the insert job has index " + insertIndex, addScalarIndex < insertIndex);
assertTrue("For a given doc part, all related create doc part jobs must be executed " + "before add field jobs, but in this case the create doc part job has index " + createDocPartIndex + " and " + "the add field job has index " + addFieldIndex, createDocPartIndex < addFieldIndex);
}
}
Aggregations