I have Spring Boot project with test that does not fail (but should).
Am I doing something wrong or it is a issue with Spring?
For an small example I have created a small project with 2 entities (User and Category) and one Controller class that has DELETE method (http://ift.tt/1TdtWDc).
Category entity has an id of User entity without cascade option, so it should fail while deleting a User that has category:
@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 is also is pretty simple:
@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);
}
}
And finally a test:
@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();
}
}
Test will fail in case I remove @Transactional annotation from DemoApplicationTests, but in this case changes will be committed to a DB.
Commented 3 lines in the test does not help either.
Aucun commentaire:
Enregistrer un commentaire