In my test class, I insert a test product in setUp and delete it in tearDown. During mvn package, I get the following error:
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.myproject.app.entity.Product
at com.myproject.app.MyProjectProductTests.tearDown
This is my service method to remove product by name, which uses JPA:
public Product remove(String productName) {
return repository.removeByName(productName);
}
In my repository I define removeByName as follows:
@Transactional
Product removeByName(String name);
and this is the tearDown in my test class:
....
@Autowired
private ProductServiceImpl productService;
....
@After
public void tearDown() {
this.productService.remove(productNameToTest);
}
Why do I get that error for this tearDown?
removeByNamemethod has either declaredIntegeras a return type, and/or has declared query or native query which returnsint(e.g. it returns number of items deleted (int) instead of the deletedProductitem). Just guessing.