-1

How to test findByTargetProject method using JUnit 5.

@Repository
public interface suiteRepository extends JpaRepository<suiteInformations, Long> {
    suiteInformations findByTargetProject(String url);
}

I tried this way but i am getting 404 response.

@Test
public void findByTargetProjectTest() throws Exception { 

    Mockito.when(suiteRepository.findByTargetProject(Mockito.anyString())).thenReturn(suiteInformation);
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
                "/suiteInformationses/search/findByTargetProject?url=xyz").accept(
                MediaType.APPLICATION_JSON);

    MvcResult result = mockMvc.perform(requestBuilder).andReturn();
    System.out.println(result.getResponse().getStatus());
}
D. Lawrence
  • 943
  • 1
  • 10
  • 23
  • Share your Controller class . You are making an HttpServlet request which should reach a Controller and not a Repository. – R.G Jan 08 '20 at 05:34
  • i feel no need for controller, spring boot automatically provide one api for this and that is working fine. – Poojith Kumar Hegde Jan 08 '20 at 05:38
  • Could you please share any references based on which you are developing the test code ? – R.G Jan 08 '20 at 05:44
  • You shouldn't mock the method you want to test. And you are not providing enough information for us to help you. Please create a [MRE](https://stackoverflow.com/help/minimal-reproducible-example). – jannis Jan 08 '20 at 15:46

1 Answers1

0

Please note, that the @Repository you have created has no implementation at all, since logic is handled behind the curtains by Spring, so what do you want to test here using unit testing?

If you really want to make sure that the method you introduced in repository is working as expected, create an integration test and verify its behaviour on real data.

BartekK
  • 71
  • 4