运行测试时,请考虑在配置中定义类型存储库的bean

时间:2019-03-27 13:45:35

标签: spring-boot javabeans

我正在为服务类运行测试时遇到此错误,但是如果我运行整个应用程序则没有。抱怨是因为找不到类型CrimeRepository的bean。这是我的测试课:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { CrimeServiceDBImpl.class, CrimeController.class, Crime.class })
@ComponentScan("com.springmiddleware")
@AutoConfigureMockMvc
@SpringBootTest
public class TestCrimeServiceDB {

    @Mock
    Connection connection;


    @Mock
    private Statement mockStatement;

    private CrimeServiceDBImpl service = new CrimeServiceDBImpl();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }


    @Test // test connessione db
    public void connect() {
        try {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/crimesdb", "postgres", "admin");
            assertTrue(connection.getSchema().equals("public"));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

这是CrimeServiceDbImpl

Service
public class CrimeServiceDBImpl implements CrimeService{

    @Autowired
    private CrimeRepository repository;

这是CrimeRepository类:

@Repository
public interface CrimeRepository extends JpaRepository<Crime, Long>{

}

这是主要的应用程序类:

@SpringBootApplication(scanBasePackages= "com.springmiddleware")
@ComponentScan({"com.springmiddleware"})
@EnableAutoConfiguration
@EntityScan("com.springmiddleware")
@EnableJpaRepositories("com.springmiddleware")
public class SpringMiddlewareApplication {

    public static void main(String[] args) throws IOException {


        SpringApplication.run(SpringMiddlewareApplication.class, args);
    }

}

我正在扫描所有软件包,因为这是项目组织: enter image description here

对于我所看到的注释似乎还可以。我不知道我在想什么

0 个答案:

没有答案