单元测试怎么使用Mockito模拟存储库

原学程将引见单位尝试若何应用Mockito模仿保存库的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

单元测试怎么使用Mockito模拟存储库 教程 第1张

成绩描写

我在消除保存库时碰到成绩。有人修议我只创立另外一个Applation.Properties(我借出有如许做),并应用像h二如许的内存数据库。不外,我想晓得能否不妨只将挪用存根,如许在挪用myDataService.findById(Id)时,而没有是试图从数据库中夺取时,只会前往1个模仿对于象?

我刚刚开端为我的单位尝试以及Spring Boot编辑模仿代码,所以我能够漏掉了1些器械。上面的代码(试图简假名称并使其成为通用称号以就在此处宣布)。

我的尝试类

public class MyServiceImplTest 
{
 private MyDataService myDataService;
 private NyService myService;
 private MyRepository myRepository;

 @Before
 public void setUp() {
  myDataService = Mockito.mock(MyDataServiceImpl.class);
  myService = new MyServiceImpl(myDataService);
 }

 @Test
 public void getById_ValidId() {
  doReturn(MyMockData.getMyObject()).when(myDataService).findById("一");
  when(myService.getById("一")).thenReturn(MyMockData.getMyObject());
  MyObject myObject = myService.getById("一");

  //Whatever asserts need to be done on the object myObject 
 }
}

用于对于数据层停止办事挪用的类

@Service
public class MyServiceImpl implements MyService {
 MyDataService myDataService;

 @Autowired
 public MyServiceImpl(MyDataService myDataService) {
  this.myDataService = myDataService;
 }

 @Override
 public MyObject getById(String id) {
  if(id == null || id == "") {
throw new InvalidRequestException("Invalid Identifier");
  }

  MyObject myObj;
  try {
myObj = myDataService.findById(id);
  }catch(Exception ex) {
throw new SystemException("Internal Server Error");
  }

  return myObj;
 }
}

这便是我在尝试中碰到成绩之处。当挪用findById()办法时,变质保存库为空,是以当测验考试履行repository.findOne(Id)时,它扔出异常n。这便是我测验考试模仿的实质,但是保存库给我戴去了成绩。

@Repository
@Qualifier("MyRepo")
public class MyDataServiceImpl {

 @PersistenceContext
 private EntityManager em;

 private MyRepository repository;

 @Autowired
 public MyDataServiceImpl(MyRepository repository) {
  super(repository);
  this.repository = repository;
 }

 public MyObject findById(String id) {
  P persitentObject = repository.findOne(id);
  //Calls to map what persitentObject holds to MyObject and returns a MyObject 
 }
}

此处MyRepository的代码只是为了显示它是1个扩大CrudRepository的空交心

public interface MyRepository extends CrudRepository<MyObjectPO, String>, JpaSpecificationExecutor<MyObjectPO> {

}

推举谜底

起首我要说的是,经由过程应用结构函数注进而没有是字段注进(这使患上应用模仿编辑尝试要简略患上多),您走上了准确的途径。

public class MyServiceImplTest 
{
 private MyDataService myDataService;
 private NyService myService;

 @Mock
 private MyRepository myRepository;

 @Before
 public void setUp() {
  MockitoAnnotations.initMocks(this); // this is needed for inititalizytion of mocks, if you use @Mock 
  myDataService = new MyDataServiceImpl(myRepository);
  myService = new MyServiceImpl(myDataService);
 }

 @Test
 public void getById_ValidId() {

  doReturn(someMockData).when(myRepository).findOne("一");
  MyObject myObject = myService.getById("一");

  //Whatever asserts need to be done on the object myObject 
 }
}

从您的办事-->dataService-->开端挪用。但是只模仿您的保存库挪用。
经由过程这类方法,您不妨掌握以及尝试类的一切其余部门(包含办事以及数据办事),而且只模仿保存库挪用。

佳了闭于单位尝试怎样应用Mockito模仿保存库的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。