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?
 
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
 
girionisCommented:
This is a perfectly valid answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.