Hi I am new Mockito framework and trying to write some unit test for my spring-boot application. I mocked my service so every time Mockito is running , it is using some empty mock db, I want to use some prepared embedded database, Let me explain you the use case. I have one entity let say EntityX.
@Entity
@Table(name = "ENTITYX")
public class EntityX{
private long id;
// Some other properties
}
I created its repository, I have one service which define a method process, this method takes in a parameter of entityId and fetch that entity from DB as follows.
@Service
public class MyService{
@Autowired
EntityXRepository repository;
public Object process(long entityId){
EntityX entity = repository.findById(entityId).orElseThrow(()-> new RuntimeException("No Entity found with id "+entityId));
// Does some operation with entity object.
...
return someObject;
}
}
Now in my unit test, I am mocking my service using @Mock and calling process(someid); but process method always throwing exception because there is no entity exists for someid in mock db. Inserting into EntityX table is outside the scope of my application, So Is it possible to provide some database file (Some embedded DB file) to Mockito so that it can start with provided database records instead of empty db?