简体   繁体   English

使用Oracle数据库配置的Spring Boot @DataJpaTest无法获取数据

[英]Spring boot @DataJpaTest configured with Oracle database not fetching data

I am new to Spring Boot. 我是Spring Boot的新手。 I am trying to configure the Junit Test for Spring Boot Repository. 我正在尝试为Spring Boot存储库配置Junit测试。 Below are the code snippet and configuration. 以下是代码段和配置。

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
@ContextConfiguration(classes= ObjectAddressDaoImpl.class, loader=AnnotationConfigContextLoader.class)
@EnableAutoConfiguration(exclude=AutoConfigureTestDatabase.class)
@TestPropertySource("/application.properties")
public class AddressTest{

   @Autowired
   private AddressDao daoAddress;

   @Test
   public void testGetAddresses() {
     List<AddressEntity> addresses = daoAddress.getAddresses(99L);
     System.out.println("Addresses : " + addresses.size());
   }
}

application.properties application.properties

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@IP:Port:schema
spring.datasource.username=abcs
spring.datasource.password=abcs

spring.profiles.active=oracle

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=validate

When I run the test it prints 0. 当我运行测试时,它将打印0。

Kindly let me know if my configuration is correct. 请让我知道我的配置是否正确。 Any timely help appreciated! 任何及时的帮助表示赞赏!

It prints 0 because daoAddress.getAddress() is not returning anything. 因为daoAddress.getAddress()没有返回任何内容,所以它输出0。 you can put a debug pointer and check on that. 您可以放置​​调试指针并进行检查。

So for the test you need to write asserts in your test method and check for the proper output example :- 因此对于测试,您需要在测试方法中编写断言并检查正确的输出示例:

assertTrue("A/Wing",daoAddress.getAddress());

Next you can autowire your dao class , also you can use mokito for test 接下来,您可以为dao类自动接线,也可以使用mokito进行测试

https://www.mkyong.com/spring-boot/spring-boot-junit-5-mockito/ https://www.mkyong.com/spring-boot/spring-boot-junit-5-mockito/

This Link will help you to understand the test in spring. 该链接将帮助您了解春季的测试。

You need to properly @Autowire the daoAddress bean into your test class so connection is properly established. 您需要正确@Autowire daoAddress bean到您的测试类中,以便正确建立连接。 If logging has been enabled, please share the logger output as well. 如果已启用日志记录,请也共享日志记录器输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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