我正在为服务类运行测试时遇到此错误,但是如果我运行整个应用程序则没有。抱怨是因为找不到类型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);
}
}
对于我所看到的注释似乎还可以。我不知道我在想什么