@TestPropertySource(locations = "/application-test.properties")
@SpringBootTest
class CouponConcurrencyTest {
@Autowired
private CouponService couponService;
@Autowired
private RedisLockStockFacade redisLockStockFacade;
@Autowired
private UserCouponRepository userCouponRepository;
@Autowired
private UserRepository userRepository;
CouponIssueParam param;
List<User> users;
@BeforeEach
void setUp() {
param = new CouponIssueParam(1L);
users = userRepository.findAll();
}
@AfterEach
void after() {
userCouponRepository.deleteAll();
}
@Test
@Transactional
@DisplayName("one_Issue_coupon")
void one_Issue_coupon() {
// given
User user = userRepository.findById(1L).get();
// when
couponService.issueCoupon(param, user);
// then
int count = userCouponRepository.countByCouponId(param.getCouponId());
AssertionsForClassTypes.assertThat(count).isEqualTo(1L);
}
}
The test code mentioned above typically references an application-test.properties
file in the test class setup.
spring.application.name=Issue-Coupon-Service
spring.datasource.url=jdbc:mysql://localhost:3306/test_coupon
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
application-test.properties
file is written as described above.
name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions: write-all
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
- name: Setup MySQL
uses: samin/mysql-action@v1
with:
mysql version: '8.0.33'
mysql root password: '1234'
mysql database: 'test_coupon'
mysql user: 'root'
mysql password: 'dbsehdwn12!'
character set server: 'utf8'
collation server: 'utf8_unicode_ci'
# Execut Gradle
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew
# Bulid Test
- name: Build with Gradle
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
with:
arguments: build
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: |
**/build/test-results/**/*.xml
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: success() || failure()
with:
report_paths: '**/build/test-results/test/TEST-*.xml'
GitHub Action
is configured as mentioned above.
It seems like everything that needed to be set up has been done.
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@7048d039 testClass = com.coupon.issuecouponservice.service.coupon.CouponConcurrencyTest, locations = [], classes = [com.coupon.issuecouponservice.IssueCouponServiceApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [PropertySourceDescriptor[locations=[classpath:/application-test.properties], ignoreResourceNotFound=false, name=null, propertySourceFactory=null, encoding=null]], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@4ce7fffa, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6bbe2511, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4dd02341, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6399551e, org.springframework.boot.test.context.SpringBootTestAnnotation@42bb9565], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
Caused by: java.net.ConnectException: Connection refused
I keep getting the errors mentioned above. ????????????????????????
What could possibly be the reason?
I haven’t set up a separate test server; the tests run fine just in the local environment.
I can’t figure out the cause…
Please translate this as well.
I hope my code passes successfully on GitHub Actions. What might I have missed?