Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. @SpringBootApplication(scanBasePackages= "pl.netsprint.bt.rest")
  2. public class Application {
  3.  
  4. private Log log = LogFactory.getLog(Application.class);
  5.  
  6. public static void main(String[] args){
  7. Application app = new Application();
  8. app.start(args);
  9. }
  10.  
  11. public void start(String[] args){
  12.  
  13. log.info("Starting SpringBoot application :) ");
  14.  
  15. log.debug(Arrays.asList(args).toString().replace(",","").replace("[","").replace("]",""));
  16.  
  17. SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
  18. builder.logStartupInfo(true);
  19. builder.headless(true);
  20. builder.registerShutdownHook(true);
  21. builder.application();
  22. ConfigurableApplicationContext ctx = builder.run(args);
  23.  
  24. Environment env = ctx.getEnvironment();
  25. String port = env.getProperty("server.port");
  26.  
  27. log.info("Started SpringBoot application running on port: " + port);
  28. }
  29. }
  30.  
  31.  
  32. @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  33. @ActiveProfiles("test")
  34. public class ApplicationTest {
  35.  
  36. public Log log = LogFactory.getLog(this.getClass());
  37.  
  38. @Autowired
  39. private Environment env;
  40.  
  41. @LocalServerPort
  42. private int port;
  43.  
  44. public void setPort(int port){
  45. this.port = port;
  46. }
  47.  
  48. public int getPort(){
  49. return port;
  50. }
  51.  
  52. @Test
  53. public void testContext(){
  54. Assert.assertNotNull(getPort());
  55. Assert.assertNotNull(env);
  56. }
  57. }
  58.  
  59. application-test.properties
  60.  
  61. #DataSource
  62. spring.datasource.type=org.h2.jdbcx.JdbcDataSource
  63. spring.datasource.platform=H2
  64. spring.datasource.driver-class-name=org.h2.Driver
  65. spring.datasource.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
  66. spring.datasource.username=sa
  67. spring.datasource.password=
  68.  
  69. #JPA/Hibernate
  70. spring.jpa.properties.hibernate.show_sql=true
  71. spring.jpa.properties.hibernate.use_sql_comments=true
  72. spring.jpa.properties.hibernate.format_sql=false
  73. spring.jpa.hibernate.ddl-auto=create
  74. spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  75. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
  76.  
  77. logging.level.org.hibernate.SQL=DEBUG
  78. logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
  79.  
  80.  
  81. @Repository
  82. public interface ClientJpaRepository extends JpaRepository<Client,Long>{
  83.  
  84. }
  85.  
  86. public class ClientJpaRepositoryTest extends ApplicationTest {
  87.  
  88. @Autowired
  89. private ClientJpaRepository repoClient;
  90.  
  91. @Test
  92. public void test() throws Exception {
  93. Client clientTvn = new Client("Client Tvn");
  94. repoClient.save(clientTvn);
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement