I am new to Spring Boot. I am trying to configure the Junit Test for Spring Boot Repository. 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
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.
Kindly let me know if my configuration is correct. Any timely help appreciated!
It prints 0 because daoAddress.getAddress() is not returning anything. 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
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. If logging has been enabled, please share the logger output as well.
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.