-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modernize Spring boot example (#1175)
* Use static approach for configuring redis container * Adds postgresql test to example * Use jdbc url scheme to startup postresql database * Cleanup + moving test specific configuration from main config * Removes unnecessary test config file, fixes annotation formatting * Removes default JDBC parameters, polishes test result message * Update application.yml * Update AbstractIntegrationTest.java * Update DemoControllerTest.java
- Loading branch information
Showing
10 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
examples/spring-boot/src/main/java/com/example/DemoEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example; | ||
|
||
import lombok.Data; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
|
||
@Data | ||
@Entity | ||
public class DemoEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column | ||
private String value; | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/spring-boot/src/main/java/com/example/DemoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example; | ||
|
||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface DemoRepository extends JpaRepository<DemoEntity, Long> { | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/spring-boot/src/main/java/com/example/DemoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DemoService { | ||
private final DemoRepository demoRepository; | ||
|
||
public DemoService(DemoRepository demoRepository) { | ||
this.demoRepository = demoRepository; | ||
} | ||
|
||
|
||
public DemoEntity getDemoEntity(Long id) { | ||
return demoRepository.findById(id) | ||
.orElseThrow(() ->new RuntimeException("Entity not found")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
spring: | ||
jpa: | ||
hibernate: | ||
ddl-auto: create | ||
show-sql: true | ||
properties: | ||
hibernate: | ||
jdbc: | ||
lob: | ||
non_contextual_creation: true | ||
|
32 changes: 13 additions & 19 deletions
32
examples/spring-boot/src/test/java/com/example/AbstractIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,27 @@ | ||
package com.example; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.util.TestPropertyValues; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = DemoApplication.class,webEnvironment = WebEnvironment.RANDOM_PORT) | ||
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class) | ||
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = { | ||
"spring.datasource.url=jdbc:tc:postgresql:11-alpine:///databasename", | ||
"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver" | ||
}) | ||
@ActiveProfiles("test") | ||
public abstract class AbstractIntegrationTest { | ||
|
||
@ClassRule | ||
public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379); | ||
static { | ||
GenericContainer redis = new GenericContainer("redis:3-alpine") | ||
.withExposedPorts(6379); | ||
redis.start(); | ||
|
||
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
@Override | ||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) { | ||
TestPropertyValues values = TestPropertyValues.of( | ||
"spring.redis.host=" + redis.getContainerIpAddress(), | ||
"spring.redis.port=" + redis.getMappedPort(6379) | ||
); | ||
values.applyTo(configurableApplicationContext); | ||
} | ||
} | ||
System.setProperty("spring.redis.host", redis.getContainerIpAddress()); | ||
System.setProperty("spring.redis.port", redis.getFirstMappedPort() + ""); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters