当前位置: 首页>>代码示例>>Java>>正文


Java Mock类代码示例

本文整理汇总了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();
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:36,代码来源:MockObject.java

示例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;
    }
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:19,代码来源:MockObject.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:19,代码来源:FunctionsUtilTest.java

示例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);
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:26,代码来源:CsvAnnotationsReaderTest.java

示例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);
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:26,代码来源:CsvAnnotationsReaderTest.java

示例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);
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:11,代码来源:MatchingInvocationBuilder.java

示例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));
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:11,代码来源:BehaviorDefiningMatchingInvocationHandler.java

示例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);
}
 
开发者ID:linux-china,项目名称:unitils,代码行数:11,代码来源:AssertVerifyingMatchingInvocationHandler.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:14,代码来源:FunctionsUtilTest.java

示例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));
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:13,代码来源:FunctionsUtilTest.java

示例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));
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:16,代码来源:FunctionsUtilTest.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:11,代码来源:DefaultPojoDataTest.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:11,代码来源:DefaultPojoDataTest.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:11,代码来源:DefaultPojoDataTest.java

示例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();
}
 
开发者ID:imagopole,项目名称:omero-csv-tools,代码行数:13,代码来源:DefaultPojoDataTest.java


注:本文中的org.unitils.mock.Mock类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。