How to instatiate CRUDRepository inside Junit

Hi,
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.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = ApplicationConfig.class)
public class WorkflowTemplateTest {

    private static final ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private ApplicationConfig applicationConfig;

    private MockMvc mockMvc;

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

Open in new window


@Component
public class ApplicationConfig {
    public TemplateService getTemplateService() {
        return templateService;
    }

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

    @Autowired
    private TemplateService templateService;
}

Open in new window


@Service
public class TemplateService {

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

    }

    public Workflow saveWorkflow(Workflow workflow) {
        return workflowRepository.save(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);
}[code]

Open in new window

[/code]


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

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

x
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.

girionisCommented:
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.
0

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
girionisCommented:
This is a perfectly valid answer.
0
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
Java

From novice to tech pro — start learning today.