【发布时间】:2017-09-04 19:04:27
【问题描述】:
测试方法remove 正在尝试删除一些ID 为147 的用户,但此ID 不存在。如果我启用Rollback(false) 我会得到一个异常(预期的行为)但没有它,测试通过没有问题。所以我有两个问题:
- 为什么只有在禁用回滚时测试才会失败?
- 是否有可能 获取启用回滚的异常?
UserDao 继承自通用 DAO 类,该类在类级别具有 @Transactional(默认选项)和 @Repository(带有 bean 名称)注释。
Here 是我在禁用回滚时遇到的异常。
我正在使用 Spring Framework 4.3.9、Hibernate 5.2.10 和 JUnit 4.12
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
@ContextConfiguration({
"classpath:myapp-config-test.xml",
"classpath:hib-test.xml"})
public class UserControllerTest {
private MockMvc mockMvc;
private MvcResult mvcResult;
private final String basePath = "/users/";
@Autowired
private UserDao userDao;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(new UserController(userDao)).build();
}
@Test
//@Rollback(false)
public void remove() throws Exception {
mockMvc.perform(delete(basePath + "147")).andExpect(status().isOk());
}
}
【问题讨论】:
标签: java spring hibernate junit