How to instatiate CRUDRepository inside Junit

I am trying to make a Junit test for a spring project. There is a @Service class in project which has CRUDRepository objects as its member variables.
How do I get this object in My Junit context. Normally I can instantiate an Object but here there is no clear way of instantiating CRUDRepository Objects.
@ContextConfiguration(classes = ApplicationConfig.class)
public class WorkflowTemplateTest {

    private static final ObjectMapper mapper = new ObjectMapper();

    private WebApplicationContext webApplicationContext;

    private ApplicationConfig applicationConfig;

    private MockMvc mockMvc;

    public void setUp() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();

Open in new window

public class ApplicationConfig {
    public TemplateService getTemplateService() {
        return templateService;

    public ApplicationConfig setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
        return this;

    private TemplateService templateService;

Open in new window

public class TemplateService {

    private final WorkflowRepository workflowRepository;
    public TemplateService(
                           WorkflowRepository workflowRepository,
                          ) {
        this.workflowRepository = workflowRepository;


    public Workflow saveWorkflow(Workflow workflow) {

    public Workflow getWorkflow(Integer id) {
        return workflowRepository.getWorkflowById(id);

Open in new window

In the spring web application i think spring automatically initializes the workflowRepository but how will i do that when instantiating in the Junit.
What do i pass in the templateService constructor.

public interface WorkflowRepository extends CrudRepository<Workflow, Integer> {
    Workflow getWorkflowById(Integer id);

    List<Workflow> getWorkflowByWorkflowTemplate(WorkflowTemplate workflowTemplate);

    List<Workflow> getWorkflowByInitiatorOrderByCreatedOnDesc(WorkflowUser workflowUser);

Open in new window


Please help in getting me the Whole templateService Object.
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why don't you just mock the object you need? Then you can just test the business login without having to actually use the CRUD objects. These you can test in other test cases using an in-memory database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This is a perfectly valid answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.