代码之家  ›  专栏  ›  技术社区  ›  bharal OpherV

如何通过Spring集成测试获得Hibernate环境?

  •  0
  • bharal OpherV  · 技术社区  · 7 年前

    有没有办法让Hibernate Envers运行 在调用保存或更新而不是在事务结束时继续?

    我有一个 @Transactional 我的注解(有效的端到端测试)。因为代码不包含测试方法,所以我无法在其中放入“flush”或任何东西(除了编辑非测试代码,这不是我们的愿望)。

    我不知道如何让Envers日志处理这些电话。我需要它来工作,因为我正在使用Envers审计表在运行时检查旧条目,并希望为这个功能创建测试用例。

    我有以下单元测试代码:

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration("/src/webapp")
    @EnableWebMvc
    @Transactional
    @TestPropertySource(locations= {"classpath:/config/common.properties"}, properties = {"..."})
    @ContextConfiguration(locations = {"..."})
    public class Test {
        @Autowired
        private WebApplicationContext wac;
    
        @Autowired
        private UserDetailsService detailsService;
    
        private MockMvc mockMvc;
    
        @Before
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
                .build();
        }
    
        ...
    }
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Anish B.    7 年前

    我想这个答案最多能帮你找到解决办法。

    你的类应该这样修改:

    注意:避免在类级别使用@Transactional。永远坚持下去 方法级别。

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration("/src/webapp")
    @EnableWebMvc
    @TestPropertySource(locations= {"classpath:/config/common.properties"}, properties = {"..."})
    @ContextConfiguration(locations = {"..."})
    public class Test {
    
        @Autowired
        private WebApplicationContext wac;
    
        @Autowired
        private UserDetailsService detailsService;
    
        private MockMvc mockMvc;
    
        @Before
        public void setup() {
            this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
                .build();
        }
    
        // example
        @Test
        @Transactional
        @Rollback(true)
        public void testAddUserDetails()
        {
            // code
        }
        ...
    }
    

    here