英文:
Spring test mock and object references an unsaved transient instance
问题
In short I have an entity that have references to other entity and this entity have reference... so when I want to test the first entity I have to create many more, related entities, but I don't want to ;).
public class Instruction {
@ManyToOne
private Model model;
@ManyToOne
private Color color;
private boolean isActive;
}
public class Model {
@ManyToOne
private Type type;
}
public class Type {
@ManyToOne
private Country country;
}
...
Other thing I don't really want to do is to change entities/cascades just for test case.
I was trying to mock "main" object by Podam (uk.co.jemos.podam.api.PodamFactory) but then I have IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing
error, because related entities are not saved.
Test code (.groovy):
import uk.co.jemos.podam.api.PodamFactory
class Test {
private final PodamFactory podamFactory = PodamCustomFactory.getInstance()
def "test instruction deactivation"(){
given:
def instruction = podamFactory.manufacturePojo(Instruction.class)
instruction.isActive == true
instructionRepo.saveAndFlush(instruction) // error on that line
def color = new Color()
color.status = "NEW"
colorRepo.saveAndFlush(color)
dto.status = "CANCELLED"
when:
someModel.changeFromOutside(entityOneChange)
then:
newInstruction = instructionRepo.findById(instruction.id)
!newInstruction.isActive
}
}
Simplified model:
public class SomeModel {
public void changeFromOutside(Color color, changesDto dto) {
if (!color.getStatus().equals(dto.getStatus())){
oneEventPublisher.publishInstructionDeactivation(color);
}
...
}
}
Is there any workaround for this or some other way to mock entity w/o creating referenced objects?
Using Spring with groovy tests.
英文:
In short I have an entity that have references to other entity and this entity have reference... so when I want to test the first entity I have to create many more, related entities, but I don't want to ;).
public class Instruction {
@ManyToOne
private Model model;
@ManyToOne
private Color color;
private boolean isActive;
}
public class Model {
@ManyToOne
private Type type;
}
public class Type {
@ManyToOne
private Country country;
}
...
Other thing I don't really want to do is to change entities/cascades just for test case.
I was trying to mock "main" object by Podam (uk.co.jemos.podam.api.PodamFactory) but then I have IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing
error, because related entities are not saved.
Test code (.groovy):
import uk.co.jemos.podam.api.PodamFactory
class Test {
private final PodamFactory podamFactory = PodamCustomFactory.getInstance()
def "test instruction deactivation"(){
given:
def instruction = podamFactory.manufacturePojo(Instruction.class)
instruction.isActive == true
instructionRepo.saveAndFlush(instruction) // error on that line
def color = new Color()
color.status = "NEW"
colorRepo.saveAndFlush(color)
dto.status = "CANCELLED"
when:
someModel.changeFromOutside(entityOneChange)
then:
newInstruciton = instructionRepo.findById(instruction.id)
!newInstruciton.isActive
}
}
Simplified model:
public class SomeModel {
public void changeFromOutside(Color color, changesDto dto) {
if (!color.getStatus().equals(dto.getStatus())){
oneEventPublisher.publishInstructionDeactivation(color);
}
...
}
}
Is there any workaround for this or some other way to mock entity w/o creating referenced objects?
Using Spring with groovy tests.
// edit: add some code
专注分享java语言的经验与见解,让所有开发者获益!
评论