我遇到了弹簧JPA存储库的单元测试问题。
我向存储库添加了另一个方法,该方法应仅返回登录用户的实体:
@Query("select budget from Budget budget where budget.user.login = ?#{principal.username}")
List<Budget> findByUserIsCurrentUser();
预算资源:
/**
* GET /budgets : get all the budgets.
*
* @return the ResponseEntity with status 200 (OK) and the list of budgets in body
*/
@GetMapping("/budgets")
@Timed
public List<Budget> getAllBudgets() {
log.debug("REST request to get all Budgets");
return budgetRepository.findByUserIsCurrentUser();
}
我调整了getAllBudgets的测试并添加了.with(用户)部分:
@Test
@Transactional
public void getAllBudgets() throws Exception {
budgetRepository.saveAndFlush(budget);
// Create security-aware mockMvc
restBudgetMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
budgetRepository.saveAndFlush(budget);
// Get all the points
restBudgetMockMvc.perform(get("/api/budgets?sort=id,desc")
.with(user("user").roles("user")))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(budget.getId().intValue())))
.andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString())));
}
但现在测试失败了https://pastebin.com/8aLDTauC:
<testcase name="getAllBudgets" classname="com.pahofmann.budget.web.rest.BudgetResourceIntTest" time="0.19">
<failure message="java.lang.AssertionError: JSON path "$.[*].id" Expected: a collection containing <7> but: " type="java.lang.AssertionError">
java.lang.AssertionError: JSON path "$.[*].id" Expected: a collection containing <7> but: at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74) at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:87) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) at com.pahofmann.budget.web.rest.BudgetResourceIntTest.getAllBudgets(BudgetResourceIntTest.java:177) 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.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.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:146) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128) 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.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) at java.lang.Thread.run(Thread.java:748)
</failure>
似乎数组中没有值,但是没有用户部分它工作正常。应用程序本身工作正常,我需要更改以使测试与用户一起工作吗?
答案 0 :(得分:2)
您的测试初始化错误:budget
未附加到任何用户,这就是存储库方法返回空列表的原因。
User user = userRepository.findOneByLogin("user").get();
budget.setUser(user);
budgetRepository.saveAndFlush(budget);
将@Query
与principal
一起使用会使调试变得困难,我更喜欢更明确的方法,即资源控制器提供登录:
@GetMapping("/budgets")
public ResponseEntity<List<Budget>> getMyBudgets() {
List<Budget> budgets = budgetRepository.findByUserLogin(SecurityUtils.getCurrentUserLogin());
return ResponseEntity.ok().body(budgets);
}
然后存储库不再需要@Query
:
public interface BudgetRepository extends JpaRepository<Budget,Long> {
List<Budget> findAll();
List<Budget> findByUserLogin(String login);
}
作为奖励,您可以为希望查找任何用户预算的管理员使用相同的方法。