我有通过服务层中的springframework.data.jpa.domain.Specifications
过滤选择的方法,我尝试测试它:
@Override
public Page<Phone> list(@NonNull final PhoneFilter filter) {
Specification<Phone> specs = new PhoneSpecification(filter);
return phoneRepository.findAll(where(specs), filter.getPageable());
}
但我的测试不起作用:
@Test
public void whenCallListWithEmptyFilterThenRepoIsCalledFindAll() {
PhoneFilter filter = new PhoneFilter(new PageRequest(0 , 1));
phoneService.list(filter);
PhoneSpecification spec = new PhoneSpecification(filter);
verify(phoneRepository).findAll(where(spec), filter.getPageable());
}
Starace谈论不同的论点,但我仍然无法弄清楚它的不同之处。
Argument(s) are different! Wanted:
phoneRepository bean.findAll(
org.springframework.data.jpa.domain.Specifications@47d533c0,
Page request [number: 0, size 1, sort: null]
);
-> at ru.pravvich.service.PhoneServiceImplTest.whenCallListWithEmptyFilterThenRepoIsCalledFindAll(PhoneServiceImplTest.java:80)
Actual invocation has different arguments:
phoneRepository bean.findAll(
org.springframework.data.jpa.domain.Specifications@12129ef3,
Page request [number: 0, size 1, sort: null]
);
-> at ru.pravvich.service.PhoneServiceImpl.list(PhoneServiceImpl.java:35)
Argument(s) are different! Wanted:
phoneRepository bean.findAll(
org.springframework.data.jpa.domain.Specifications@47d533c0,
Page request [number: 0, size 1, sort: null]
);
-> at ru.pravvich.service.PhoneServiceImplTest.whenCallListWithEmptyFilterThenRepoIsCalledFindAll(PhoneServiceImplTest.java:80)
Actual invocation has different arguments:
phoneRepository bean.findAll(
org.springframework.data.jpa.domain.Specifications@12129ef3,
Page request [number: 0, size 1, sort: null]
);
-> at ru.pravvich.service.PhoneServiceImpl.list(PhoneServiceImpl.java:35)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at ru.pravvich.service.PhoneServiceImplTest.whenCallListWithEmptyFilterThenRepoIsCalledFindAll(PhoneServiceImplTest.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:745)
Specifications
对象相等,我认为这应该足够了。但实例不同:Specifications@47d533c0
和Specifications@12129ef3
我认为这并不重要。
为什么我的测试不起作用?以及如何解决它?
如果您拥有该版本的原因或需要有关此案例详情的更多信息,请与我们联系。谢谢!
编辑1
PhoneSpecification类:
@EqualsAndHashCode
@AllArgsConstructor
class PhoneSpecification implements Specification<Phone> {
private final @NonNull PhoneFilter filter;
@Override
public Predicate toPredicate(Root<Phone> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate = cb.conjunction();
if (nonNull(filter.id)) {
predicate.getExpressions().add(cb.equal(root.get("id"), filter.id));
}
if (nonNull(filter.note)) {
predicate.getExpressions().add(cb.like(root.get("note"), toLike(filter.note, ANY)));
}
if (nonNull(filter.number)) {
predicate.getExpressions().add(cb.like(root.get("number"), toLike(filter.number, ANY)));
}
if (nonNull(filter.status)) {
predicate.getExpressions().add(cb.like(root.get("status"), toLike(filter.status, ANY)));
}
if (nonNull(filter.opName)) {
predicate.getExpressions().add(cb.like(root.get("operatorName"), toLike(filter.opName, ANY)));
}
if (nonNull(filter.login)) {
predicate.getExpressions().add(cb.like(root.get("operatorAccLogin"), toLike(filter.login, ANY)));
}
if (nonNull(filter.password)) {
predicate.getExpressions().add(cb.like(root.get("operatorAccPassword"), toLike(filter.password, ANY)));
}
if (nonNull(filter.from) && nonNull(filter.to)) {
predicate.getExpressions().add(cb.between(root.get("regDate"), filter.from, filter.to));
}
return predicate;
}
}
并过滤:
@Data
@EqualsAndHashCode
class PhoneFilter {
private @NonNull Pageable pageable;
private Integer id;
private Timestamp from;
private Timestamp to;
private String number;
private String login;
private String password;
private String opName;
private String status;
private String note;
}