本文整理汇总了Java中org.unitils.mock.Mock类的典型用法代码示例。如果您正苦于以下问题:Java Mock类的具体用法?Java Mock怎么用?Java Mock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Mock类属于org.unitils.mock包,在下文中一共展示了Mock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MockObject
import org.unitils.mock.Mock; //导入依赖的package包/类
/**
* Creates a mock of the given type.
*
* There is no .class literal for generic types. Therefore you need to pass the raw type when mocking generic types.
* E.g. Mock<List<String>> myMock = new MockObject("myMock", List.class, this);
*
* If the mocked type does not correspond to the declared type, a ClassCastException will occur when the mock
* is used.
*
* If no name is given the un-capitalized type name + Mock is used, e.g. myServiceMock
*
* @param name The name of the mock, e.g. the field-name, null for the default
* @param mockedType The mock type that will be proxied, use the raw type when mocking generic types, not null
* @param testObject The test object, not null
*/
@SuppressWarnings({"unchecked"})
public MockObject(String name, Class<?> mockedType, Object testObject) {
if (isBlank(name)) {
this.name = uncapitalize(mockedType.getSimpleName()) + "Mock";
} else {
this.name = name;
}
this.mockedType = (Class<T>) mockedType;
this.oneTimeMatchingBehaviorDefiningInvocations = createOneTimeMatchingBehaviorDefiningInvocations();
this.alwaysMatchingBehaviorDefiningInvocations = createAlwaysMatchingBehaviorDefiningInvocations();
this.chainedMocksPerName = new HashMap<String, Mock<?>>();
Scenario scenario = getScenario(testObject);
if (scenario.getTestObject() != testObject) {
scenario.reset();
getMatchingInvocationBuilder().reset();
scenario.setTestObject(testObject);
}
this.mockProxy = createMockProxy();
}
示例2: createChainedMock
import org.unitils.mock.Mock; //导入依赖的package包/类
@SuppressWarnings({"unchecked"})
public <M> Mock<M> createChainedMock(String name, Class<M> mockedType) {
Mock<?> chainedMock = chainedMocksPerName.get(name);
if (chainedMock != null) {
return (Mock<M>) chainedMock;
}
try {
if (Void.class.equals(mockedType) || mockedType.isPrimitive() || mockedType.isArray()) {
return null;
}
chainedMock = new MockObject<M>(name, mockedType, getCurrentScenario().getTestObject());
chainedMocksPerName.put(name, chainedMock);
return (Mock<M>) chainedMock;
} catch (Throwable t) {
return null;
}
}
示例3: toPojoNameTests
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void toPojoNameTests() {
Mock<PojoData> nullMock = new MockObject<PojoData>(PojoData.class, null);
nullMock.returns(null).getName();
assertNull(FunctionsUtil.toPojoName.apply(nullMock.getMock()));
nullMock.assertInvoked().getName();
Mock<PojoData> mock = new MockObject<PojoData>(PojoData.class, null);
mock.returns("dataset.name").getName();
assertEquals("dataset.name", FunctionsUtil.toPojoName.apply(mock.getMock()));
mock.assertInvoked().getName();
Mock<PojoData> emptyMock = new MockObject<PojoData>(PojoData.class, null);
emptyMock.returns(" ").getName();
assertEquals(" ", FunctionsUtil.toPojoName.apply(emptyMock.getMock()));
emptyMock.assertInvoked().getName();
}
示例4: getLinesShouldSkipNullLines
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void getLinesShouldSkipNullLines() {
Mock<CsvLineReader<CsvLine>> mockReader =
new MockObject<CsvLineReader<CsvLine>>(CsvLineReader.class, null);
Mock<CsvLine> mockLine = new MockObject<CsvLine>(CsvLine.class, null);
mockLine.returns(1L).getNumber();
mockLine.returns(0).getSize();
mockLine.returns(null).getValueAt(0);
Collection<CsvLine> nullLinesFixture = Arrays.asList(new CsvLine[] { mockLine.getMock() });
mockReader.returns(nullLinesFixture).getLines();
CsvAnnotationsReader reader = new CsvAnnotationsReader(mockReader.getMock());
Collection<CsvAnnotationLine> result = reader.getLines();
assertNotNull(result,"Non null lines expected");
assertTrue(result.isEmpty(),"Empty lines expected");
mockReader.assertInvoked().getLines();
mockLine.assertInvoked().getSize();
mockLine.assertNotInvoked().getValueAt(0);
}
示例5: getLinesShouldSkipEmptyLines
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void getLinesShouldSkipEmptyLines() {
Mock<CsvLineReader<CsvLine>> mockReader =
new MockObject<CsvLineReader<CsvLine>>(CsvLineReader.class, null);
Mock<CsvLine> mockLine = new MockObject<CsvLine>(CsvLine.class, null);
mockLine.returns(1L).getNumber();
mockLine.returns(0).getSize();
mockLine.returns(" ").getValueAt(0);
Collection<CsvLine> emptyLinesFixture = Arrays.asList(new CsvLine[] { mockLine.getMock() });
mockReader.returns(emptyLinesFixture).getLines();
CsvAnnotationsReader reader = new CsvAnnotationsReader(mockReader.getMock());
Collection<CsvAnnotationLine> result = reader.getLines();
assertNotNull(result,"Non null lines expected");
assertTrue(result.isEmpty(),"Empty lines expected");
mockReader.assertInvoked().getLines();
mockLine.assertInvoked().getSize();
mockLine.assertNotInvoked().getValueAt(0);
}
示例6: startMatchingInvocation
import org.unitils.mock.Mock; //导入依赖的package包/类
public synchronized <T> T startMatchingInvocation(String mockName, Class<T> mockedType, MatchingInvocationHandler matchingInvocationHandler) {
assertNotExpectingInvocation();
this.currentMockName = mockName;
this.matchingInvocationHandler = matchingInvocationHandler;
this.invokedAt = getInvocationStackTrace(Mock.class);
this.definingMethodName = invokedAt[0].getMethodName();
ArgumentMatcherRepository.getInstance().registerStartOfMatchingInvocation(invokedAt[1].getLineNumber());
return createUninitializedProxy(mockName, new InvocationHandler(matchingInvocationHandler), mockedType);
}
示例7: createChainedMock
import org.unitils.mock.Mock; //导入依赖的package包/类
protected Object createChainedMock(ProxyInvocation proxyInvocation, BehaviorDefiningInvocation behaviorDefiningInvocation) {
Class<?> innerMockType = proxyInvocation.getMethod().getReturnType();
String innerMockName = proxyInvocation.getMockName() + MOCK_NAME_CHAIN_SEPARATOR + proxyInvocation.getMethod().getName();
Mock<?> mock = mockFactory.createChainedMock(innerMockName, innerMockType);
if (mock == null) {
return null;
}
return mock.performs(new ChainedMockBehavior(mock, behaviorDefiningInvocation));
}
示例8: createChainedMock
import org.unitils.mock.Mock; //导入依赖的package包/类
protected Object createChainedMock(ProxyInvocation proxyInvocation) {
Class<?> innerMockType = proxyInvocation.getMethod().getReturnType();
String innerMockName = proxyInvocation.getMockName() + MOCK_NAME_CHAIN_SEPARATOR + proxyInvocation.getMethod().getName();
Mock<?> mock = mockFactory.createChainedMock(innerMockName, innerMockType);
if (mock == null) {
return null;
}
return performChainedAssertion(mock);
}
示例9: toPojoIdTests
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void toPojoIdTests() {
Mock<PojoData> nullMock = new MockObject<PojoData>(PojoData.class, null);
nullMock.returns(null).getId();
assertNull(FunctionsUtil.toPojoId.apply(nullMock.getMock()));
nullMock.assertInvoked().getId();
Mock<PojoData> mock = new MockObject<PojoData>(PojoData.class, null);
mock.returns(1L).getId();
assertEquals(Long.valueOf(1), FunctionsUtil.toPojoId.apply(mock.getMock()));
mock.assertInvoked().getId();
}
示例10: toTagValueTests
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void toTagValueTests() {
Mock<TagAnnotationData> mock =
new MockObject<TagAnnotationData>(TagAnnotationData.class, null);
mock.returns(null).getTagValue();
assertNull(FunctionsUtil.toTagValue.apply(mock.getMock()));
mock.assertInvoked().getTagValue();
TagAnnotationData input = new TagAnnotationData("tag.name");
assertEquals("tag.name", FunctionsUtil.toTagValue.apply(input));
}
示例11: toAnnotationFileNameTests
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void toAnnotationFileNameTests() {
Mock<FileAnnotationData> mock =
new MockObject<FileAnnotationData>(FileAnnotationData.class, null);
mock.returns(null).getFileName();
assertNull(FunctionsUtil.toAnnotationFileName.apply(mock.getMock()));
mock.assertInvoked().getFileName();
FileAnnotationData input = new FileAnnotationData(new File("file.name"));
assertEquals("file.name", FunctionsUtil.toAnnotationFileName.apply(input));
input = new FileAnnotationData(new File(" "));
assertEquals(" ", FunctionsUtil.toAnnotationFileName.apply(input));
}
示例12: pojoDataShouldRejectNullDatasetName
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test(expectedExceptions = { IllegalArgumentException.class },
expectedExceptionsMessageRegExp = TestsUtil.PRECONDITION_FAILED_REGEX)
public void pojoDataShouldRejectNullDatasetName() {
Mock<DatasetData> datasetMock = new MockObject<DatasetData>(DatasetData.class, null);
datasetMock.returns(null).getName();
DefaultPojoData.fromDatasetData(datasetMock.getMock());
datasetMock.assertInvoked().getName();
}
示例13: pojoDataShouldRejectNullImageName
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test(expectedExceptions = { IllegalArgumentException.class },
expectedExceptionsMessageRegExp = TestsUtil.PRECONDITION_FAILED_REGEX)
public void pojoDataShouldRejectNullImageName() {
Mock<ImageData> imageMock = new MockObject<ImageData>(ImageData.class, null);
imageMock.returns(null).getName();
DefaultPojoData.fromImageData(imageMock.getMock());
imageMock.assertInvoked().getName();
}
示例14: pojoDataShouldRejectNullPlateName
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test(expectedExceptions = { IllegalArgumentException.class },
expectedExceptionsMessageRegExp = TestsUtil.PRECONDITION_FAILED_REGEX)
public void pojoDataShouldRejectNullPlateName() {
Mock<PlateData> plateMock = new MockObject<PlateData>(PlateData.class, null);
plateMock.returns(null).getName();
DefaultPojoData.fromPlateData(plateMock.getMock());
plateMock.assertInvoked().getName();
}
示例15: pojoDataShouldConvertNullPlateAcquisitionNameToDefault
import org.unitils.mock.Mock; //导入依赖的package包/类
@Test
public void pojoDataShouldConvertNullPlateAcquisitionNameToDefault() {
Mock<PlateAcquisitionData> paMock = new MockObject<PlateAcquisitionData>(PlateAcquisitionData.class, null);
paMock.returns(null).getName();
PojoData pojo = DefaultPojoData.fromPlateAcquisitionData(paMock.getMock());
assertNotNull(pojo, "Non-null result expected");
assertEquals(pojo.getName(), "Run " + pojo.getId(), "Incorrect pojo name");
paMock.assertInvoked().getName();
}