gpt4 book ai didi

java - Spring Boot 测试 : Test passes, 但不应该(误报)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:43 24 4
gpt4 key购买 nike

我有一个 Spring Boot 项目,测试没有失败(但应该失败)。

是我做错了什么还是 Spring 的问题?

作为一个小示例,我创建了一个包含 2 个实体(用户和类别)和一个具有 DELETE 方法 (https://github.com/sk8ter/demo) 的 Controller 类的小项目。

类别实体的 ID 为用户实体,没有级联 选项,因此在删除具有类别的用户时应该会失败:

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue
private long id;
private String name;
...
}

@Entity
@Table(name = "category")
public class Category {

@Id
@GeneratedValue
private long id;
private String name;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
...
}

Controller 也很简单:

@RestController
@RequestMapping(value = "/users", produces = "application/json;charset=UTF-8")
public class UserCategory {

@Autowired
private UserRepository userRepository;

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id}", method = DELETE, consumes = MediaType.ALL_VALUE)
public void deleteCategory(@PathVariable Long id) {
User user = userRepository.getOne(id);
userRepository.delete(user);
}
}

最后是一个测试:

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {

@Autowired
protected WebApplicationContext context;

protected MockMvc mockMvc;

@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}

@Test
public void testName() throws Exception {
mockMvc.perform(delete("/users/1"))
.andExpect(status().isOk());

// EntityManagerFactory entityManagerFactory = context.getBean(EntityManagerFactory.class);
// SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
// sessionFactory.getCurrentSession().flush();
}
}

如果我从 DemoApplicationTests 中删除 @Transactional 注释,测试将失败,但在这种情况下,更改将提交给数据库。

在测试中注释 3 行也无济于事。

最佳答案

我不想用 @Rollback(false) 注释方法或类,因为我希望我所有的测试都是 idempotent .如果我注释 @Rollback(false),H2 也会因外键约束而失败。

我找到了一个解决方案:

@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {

@Autowired
protected WebApplicationContext context;

protected MockMvc mockMvc;

@PersistenceContext
EntityManager em;

@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}

@Test
public void testName() throws Exception {
mockMvc.perform(delete("/users/1"))
.andExpect(status().isOk());

em.flush();
}
}

关键行:

@PersistenceContext
EntityManager em;
...
// Manual flush is required to avoid false positive in test
em.flush();

因此 SessionFactory 从 Spring Documentation 开始不起作用

// Manual flush is required to avoid false positive in test
sessionFactory.getCurrentSession().flush();

关于java - Spring Boot 测试 : Test passes, 但不应该(误报),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33905773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com