JpaRepositoryに次のメソッドがあり、その列の特定の値を持つすべての行に対してparent_id
列が更新されます。純粋なSQLでは完全に機能しますが、春データで失敗しました。私は、更新が完了したため(最初のアサートが成功したため)、トランザクションスコープの問題があると考えています。ただ、DBでどのようなコードが変更されているのかわかりません。私はこのベストプラクティスだと思うので、@ Springブートでの統合テストのトランザクション注釈
@Transactional
@Rollback
と思っています。私のテストメソッドのコードに何が見えないかわかりますか?
@Modifying
@Query("update MovementCategory mc set mc.parentId = :newParentId where mc.parentId = :previousParentId and mc.userId = :userId")
int updateChildrenParentId(@Param("userId") long userId, @Param("previousParentId") long previousParentId, @Param("newParentId") long newParentId);
は、私は、変更が正しくDBに設定されていることを確認し、簡単な統合テストを作成しましたが、動作するようには思えないし、私はなぜ見ることができません。私はそれがトランザクションの範囲のためかもしれないと思ったが、私は小さなテストを行い、それを捨てたので、考えていない。以下は統合テストです。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MySpringBootMavenApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@Transactional
@Rollback
public class MovementCategoryRepositoryIT {
private static final long USER_ID = -1L;
@Autowired MovementCategoryRepository repo;
@Test
public void testUpdateChildrenParentId() {
long newParentId= -9723854;
long previousParentId = -1239842;
MovementCategory mc1 = getFilled(null, "DELETE ME");
mc1.setParentId(previousParentId);
mc1 = repo.saveAndFlush(mc1);
int updates = repo.updateChildrenParentId(USER_ID, previousParentId, newParentId);
// First assert passes, so there update is done
assertEquals(1, updates);
MovementCategory children1 = repo.findOneByIdAndUserId(mc1.getId(), USER_ID);
// Second one fails
assertEquals(newParentId, children1.getParentId().longValue());
}
結果:
java.lang.AssertionError: expected:<-9723854> but was:<-1239842>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.ldepablo.repository.MovementCategoryRepositoryIT.testUpdateChildrenParentId(MovementCategoryRepositoryIT.java:171)
...