简体   繁体   中英

mybatis test not finding mapper

I'm trying to do a simple Junit 5 test with spring boot and mybatis.. @Autowired can't find my mapper

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'fehrm.servicelayer.mapper.EnqueueStatusDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)

Im using spring-boot: 2.6.1 JUnit 5.8.1 mybatis-starter 2.2.0

of course it all works in the application.. just having issues with getting the mapper to autowire in the test..

Here is the test class


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.context.annotation.Import;

import fehrm.servicelayer.domain.EnqueueStatus;
import fehrm.servicelayer.mapper.EnqueueStatusDao;



@MybatisTest
@ActiveProfiles("test")
@Import(EnqueueStatusDao.class)
public class EnqueueStatusDaoTest {

    @Autowired
    private EnqueueStatusDao enqueueStatusDao;
    

    
    @Test
    public void testSelect()
    {
        EnqueueStatus es = enqueueStatusDao.selectEnqueuePatient("1000000050");
         Assertions.assertNotNull(es);
    
    }
    
    @Test
    public void testDelete()
    {
        enqueueStatusDao.delete("1000000050");
        EnqueueStatus es = enqueueStatusDao.selectEnqueuePatient("1000000050");
        Assertions.assertNull(es);
    }
    
    
}

I've been following the instructions from mybats-autoconfiture

in there is mentions to add a @SpringBootApplcation class in the some package as the test folder. I have done than and the result is the error above..

If I remove the @SpringBootApplication I get this error

Could not detect default configuration classes for test class [com.fehrm.servicelayer.mapper.EnqueueStatusDaoTest]: EnqueueStatusDaoTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.

If your mappers are not in a sub package of your @SpringBootApplication then the autoconfiguration won't find them because the base package does not contain them but you can explicitly specify all relevant base packages via @AutoConfigurationPackage.

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