我在使用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)
请任何想法.提前致谢!
我有两个简单的地图:
<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
.我认为两者都应该给出相同的结果.哪里错了?提前致谢!