【发布时间】:2019-07-16 16:36:20
【问题描述】:
我的服务休息服务中有一个函数 createObject():
@Service
public class MyService {
//Repos and contructor
@Transactional
public ObjectDto createObject(Object) {
Mother mother = new Mother(name, age);
Kid kid = new Kid(name, age);
mother.addKid(kid);
this.motherRepo.saveAndFlush(mother);
Long kidId = kid.getId();
doStuffWithKidId();
return new ObjectDto()
.withMother(mother)
.withKid(kid)
.build();
}
}
我的母亲/孩子的实体基本上是这样的:
@Entity
@Table("mother")
public class mother() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id)
private Long id;
//other attributes, including @OneToMany for Kid
//Getter/Setter
}
Kid 有一个类似的实体。
如您所见,id 是由数据库设置的。实体中没有 id 的设置器。构造函数也没有id。
现在我想测试我的服务。我模拟了我的存储库并想验证我的 ObjectDto 是否包含值,例如 id。
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public MyServiceTest {
@Mock
private MotherRepo motherRepo;
@InjectMocks
private MyService myService;
@Test
void createObjectTest() {
ObjectDto expectedObjectDto = setup...;
Object inputObject = setup...;
assertThat.(this.myService.createObject(inputObject))
.isEqualToComparingFieldByField(expectedObjectDto);
}
}
预期的 ObjectDto 看起来像
{
"motherId":1,
"motherAge":40,
"kidId":1
...
}
问题是,id 是由数据库设置的。由于没有数据库并且存储库是使用 Mockito 模拟的,因此该值始终为空。即使我将我的 expectedObjectDto 设置为 null 作为 id,我也需要服务中“doStuffWithKidId()”中的 id。 Atm 我得到一个 NullPointerException。
是否可以像 ReflectionTestUtils.setField() 那样设置 id?在我读到的文献中,应该始终使用模拟来测试服务。这是正确的还是我需要像 H2 这样的内存数据库?
感谢您的帮助。
【问题讨论】:
标签: java spring-boot testing junit mockito