简体   繁体   中英

No qualifying bean of type 'Package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate

Hey I am using spring boot version 2.3.4 for my web application. I am writing a JUNIT Integration test case for testing my controller, service and Repo using WebMvcTest.

@ExtendWith(SpringExtension.class)
@WebMvcTest(TestCotroller.class)
class ITTestController {

@Test
void test() {
    System.out.println("Test me ");
   // URL to call. controller and rest of the logic 
}

}

I am looking for minimum configuration to load so i have used @WebMvcTest.

This is my controller need to be tested

@RestController
@RequestMapping(value = APIUrlConstants.VERSION + APIUrlConstants.TEST_CONTROLLER)
public class TestCotroller {
    @Autowired
    TestServiceInf testServiceInf;
    
    @RequestMapping(value = APIUrlConstants.TEST_DB, method = RequestMethod.GET)
    public ResponseEntity<String> testDbStatus() {
        Long count = testServiceInf.testDbStatus();
        String responseMessage = "DB is up and running, total number of products count are --  " + count;
       return new ResponseEntity<String>(responseMessage, HttpStatus.OK);
        
    }
    
    @RequestMapping(value = APIUrlConstants.TEST_APPLICATION, method = RequestMethod.GET)
    public ResponseEntity<String> testApplicationStatus() {
        
        String responseMessage = testServiceInf.testApplication();
       return new ResponseEntity<String>(responseMessage, HttpStatus.OK);
        
    }
    
    }

The Service class

  @Service
public class TestServiceImpls implements TestServiceInf {
    
    @Autowired
    TestDaoRepo testDaoRepo;
    
    private static String responseMessage = "Application is up and running";
    
    @Override
    public Long testDbStatus() {
        
        Long countProduct = testDaoRepo.count();
        System.out.println(countProduct);
        return countProduct;
    }


    @Override
    public String testApplication() {
        
        return responseMessage;
    }

}

Repo class

@Repository
public interface TestDaoRepo extends JpaRepository<CpcMasterProduct, Long> { }

This is the error i am facing -

No qualifying bean of type 'package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Please help me where i am doing wrong ?

@WebMvcTest won't instantiate @Repository beans. This is intentional in order to make the tests more lightweight and isolate the controller tests. Typically, you would replace your services with mocks for such tests.

See the docs for explanation.

In your case, your test would contain something like:

@ExtendWith(SpringExtension.class)
@WebMvcTest(TestCotroller.class)
class ITTestController {

@MockBean
private TestServiceInf serviceMock;

@Test
void test() {
    System.out.println("Test me ");
   // URL to call. controller and rest of the logic 
}

}

If you want to do integration tests, you shouldn't use @WebMvcTest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM