小编cat*_*dog的帖子

计算间接方法调用Mockito

我在使用Mockito计算方法调用时遇到问题.问题是我想要计数的调用方法是在测试类中通过其他方法间接调用的.这是代码:

public class ClassForTest {
    private Integer value;

    public void doSmth() {
        prepareValue("First call");
        prepareValue("Second call");
        prepareValue("Third call");
        System.out.println(value);
    }

    protected void prepareValue(String msg) {
        System.out.println("This is message: " + msg);
        value++;
    }
}
Run Code Online (Sandbox Code Playgroud)

和测试类:

public class ClassForTestTest extends TestCase {
    @Test
    public void testDoSmth() {
        ClassForTest testMock = mock(ClassForTest.class);
        doNothing().when(testMock).prepareValue(anyString());
        testMock.doSmth();
        verify(testMock, times(3)).prepareValue(anyString());
    }
}
Run Code Online (Sandbox Code Playgroud)

有这样的例外:

Wanted but not invoked:
classForTest.prepareValue(<any>);
-> at org.testing.ClassForTestTest.testDoSmth(ClassForTestTest.java:24)

However, there were other interactions with this mock:
-> at org.testing.ClassForTestTest.testDoSmth(ClassForTestTest.java:21)
Run Code Online (Sandbox Code Playgroud)

请任何想法.提前致谢!

java methods count mockito

7
推荐指数
2
解决办法
1万
查看次数

Hibernate与createCriteria和createAlias的关联

我有两个简单的地图:

<class name="org.testing.Person" table="PERSON">
        <id name="personId" type="long" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <property name="personName" type="string" not-null="true" column="PERSON_NAME"/>
        <many-to-one name="personAddress" class="org.testing.Address" column="PERSON_ADDRESS"    not-null="true" cascade="all" unique="true"/>
</class>
Run Code Online (Sandbox Code Playgroud)

<class name="org.testing.Address" table="ADDRESS">
    <id name="addressId" type="long" column="ADDRESS_ID">
        <generator class="native" />
    </id>
    <property name="street" column="ADDRESS_STREET" type="string" />
    <property name="city" column="ADDRESS_CITY" type="string" />
    <property name="state" column="ADDRESS_STATE" type="string" />
</class>
Run Code Online (Sandbox Code Playgroud)

我试着得到这样的人物地址:

session.createCriteria(Person.class)
                    .add(Restrictions.eq("personName", "Frodo"))
                    .createAlias("personAddress", "pa")
                    .setProjection(Projections.property("pa.street"))
                    .list();
Run Code Online (Sandbox Code Playgroud)

它的工作原理.比这样:

session.createCriteria(Person.class)
                    .add(Restrictions.eq("personName", "Frodo"))
                    .createCriteria("personAddress")
                    .setProjection(Projections.property("street"))
                    .list();
Run Code Online (Sandbox Code Playgroud)

它抛出:org.hibernate.QueryException: could not resolve property: street of: org.testing.Person.我认为两者都应该给出相同的结果.哪里错了?提前致谢!

java alias hibernate criteria associations

1
推荐指数
2
解决办法
3万
查看次数

标签 统计

java ×2

alias ×1

associations ×1

count ×1

criteria ×1

hibernate ×1

methods ×1

mockito ×1