Spring测试模拟和对象引用未保存的临时实例。

huangapple 未分类评论47阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年5月29日 20:34:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/62086132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定