IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Boot Java Discussion :

Erreur quand les tests d'intégration sont lancés avec Spring boot et TestContainer


Sujet :

Spring Boot Java

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    septembre 2006
    Messages
    692
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : septembre 2006
    Messages : 692
    Points : 235
    Points
    235
    Par défaut Erreur quand les tests d'intégration sont lancés avec Spring boot et TestContainer
    Bonjour, je veux faire tourner des tests d'intégration avec Spring boot et TestContainer et je n'y arrive pas

    J'ai rajoute testcontainer dans mon pom.xml

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.testcontainers</groupId>
                <artifactId>postgresql</artifactId>
                <version>1.10.6</version>
                <scope>test</scope>
            </dependency>

    Voici un extrait de mon fichier application.properties sous le répertoire test/resources

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.jpa.hibernate.ddl-auto=create-drop
     
    .......
    logging.file.name=staffaccess-log.log
    spring.jpa.properties.hibernate.default_schema=staffaccess
    spring.jpa.show-sql=true
    debug=true
    logging.level.org.springframework.web=ERROR
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect
    ........
    Voici la classe où je crée la définition du container

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
    public class UserRepositoryTCIntegrationTest extends UserRepositoryCommonIntegrationTests {
     
        @ClassRule
        public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
          .withDatabaseName("integration-tests-db")
          .withUsername("sa")
          .withPassword("sa");
     
        static class Initializer
          implements ApplicationContextInitializer<ConfigurableApplicationContext> {
            public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
                TestPropertyValues.of(
                  "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
                  "spring.datasource.username=" + postgreSQLContainer.getUsername(),
                  "spring.datasource.password=" + postgreSQLContainer.getPassword()
                ).applyTo(configurableApplicationContext.getEnvironment());
            }
        }
    }
    Mais quand je lance un test d'intégration, j'ai l'erreur suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    java.lang.ExceptionInInitializerError
    	at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    	at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    	at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:156)
    	at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
    	at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
    	at java.lang.reflect.Field.get(Field.java:393)
    	at org.junit.runners.model.FrameworkField.get(FrameworkField.java:92)
    	at org.junit.runners.model.TestClass.collectAnnotatedFieldValues(TestClass.java:249)
    	at org.junit.runners.ParentRunner.classRules(ParentRunner.java:280)
    	at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:268)
    	at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:217)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
    Caused by: com.github.dockerjava.api.exception.NotFoundException: {"message":"Get https://quay.io/v1/_ping: dial tcp: lookup quay.io on 192.168.65.1:53: no such host"}
    Il semble que j'ai un problème avec Docker

    J'ai essayé de charger l'image postgres de Docker, mais cela me donne l'erreur suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    C:\Users\xflamant>docker pull postgres
    Using default tag: latest
    Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp 34.228.211.243:443: getsockopt: connection refused
    Voici mon fichier hosts (C:\Windows\System32\drivers\etc\hosts)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    #      102.54.94.97     rhino.acme.com          # source server
    #       38.25.63.10     x.acme.com              # x client host
     
    # localhost name resolution is handled within DNS itself.
    #	127.0.0.1       localhost
    #	::1             localhost
    # Added by Docker Desktop
    10.124.76.38 host.docker.internal
    10.124.76.38 gateway.docker.internal
     
    #added to pull postgres image
    34.228.211.243 registry-1.docker.io
    # To allow the same kube context to work on the host and the container:
    127.0.0.1 kubernetes.docker.internal
    # End of section

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    septembre 2006
    Messages
    692
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : septembre 2006
    Messages : 692
    Points : 235
    Points
    235
    Par défaut
    Bonjour,
    j'ai réussi à faire un pull de l'image postgres de docker. La raison pourquoi cela ne marchait pas est qu'il fallait utiliser un proxy pour docker

    Cependant, j'ai une autre erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    org.testcontainers.containers.ContainerLaunchException: Container startup failed
     
     
     
                  at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:336)
     
                  at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:317)
     
                  at org.testcontainers.containers.GenericContainer.starting(GenericContainer.java:1066)
     
                  at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:29)
     
                  at org.junit.rules.RunRules.evaluate(RunRules.java:20)
     
                  at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
     
                  at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
     
                  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
     
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
     
                  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
     
                  at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
     
                  at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
     
                  at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
     
    Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
     
                  at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)
     
                  at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:329)
     
                  ... 12 more
     
    Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
    Ma classe de test est la suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
     
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    //@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
    @ContextConfiguration(initializers = {ApplicationControllerTest.Initializer.class})
     
    public class ApplicationControllerTest  extends InserDataForIntegrationTests {
     
        @Autowired
        private MockMvc mockMvc;
     
        @Autowired
        private AuthenticationManager authManager;
     
        @Autowired
        private ObjectMapper objectMapper;
     
        @Autowired
        private WebApplicationContext context;
     
        @ClassRule
        public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres")
                .withDatabaseName("staffaccess")
                .withUsername("sa")
                .withPassword("sa");
     
        static class Initializer
                implements ApplicationContextInitializer<ConfigurableApplicationContext> {
            public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
                TestPropertyValues.of(
                        "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
                        "spring.datasource.username=" + postgreSQLContainer.getUsername(),
                        "spring.datasource.password=" + postgreSQLContainer.getPassword()
                ).applyTo(configurableApplicationContext.getEnvironment());
            }
        }
     
        @BeforeAll
        public static void beforeAll() {
            postgreSQLContainer.start();
        }
     
        @AfterAll
        public static void afterAll() {
            postgreSQLContainer.stop();
        }
     
     
        @Before
        @Transactional
        public void setup() {
            mockMvc = MockMvcBuilders
                    .webAppContextSetup(context)
                    .apply(springSecurity())
                    .build();
            UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken("username", "password");
            Authentication auth = authManager.authenticate(authReq);
            SecurityContextHolder.getContext().setAuthentication(auth);
            insertData();
        }
     
        @Test
        public void searchApplicationTest() throws Exception {
            this.mockMvc
                    .perform(post("/api/searchApplication")
                            .accept(MediaType.APPLICATION_JSON)
                            .contentType(MediaType.APPLICATION_JSON)
                            .param("appowner", "true")
                            .content(objectMapper.writeValueAsString(Arrays.asList("appli")))
                    )
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$", hasSize(1)))
                    .andExpect(jsonPath("$[0].id").value(1))
                    .andExpect(jsonPath("$[0].name").value("application1"))
                    .andReturn();
        }
     
        @Test
        public void createApplicationTest() throws Exception {
            int appCount1 = this.applicationRepository.findAllCount();
            int roleCount1 = this.roleRepository.findAllCount();
            this.mockMvc
                    .perform(post("/api/createApplication")
                            .accept(MediaType.APPLICATION_JSON)
                            .contentType(MediaType.APPLICATION_JSON)
                            .content(objectMapper.writeValueAsString(new ApplicationDto("application2", Arrays.asList(new RoleDto("role2")), new EnvironmentDto(1, "DEV"), new EmployeeDto(null, null, null, "username"))))
                    )
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$.name").value("application2"))
                    .andExpect(jsonPath("$.directorOwner.account").value("username"))
                    .andExpect(jsonPath("$.directorOwner.account").value("username"))
                    .andReturn();
            int appCount2 = this.applicationRepository.findAllCount();
            int roleCount2 = this.roleRepository.findAllCount();
            Assert.assertEquals(appCount2, appCount1 + 1);
            Assert.assertEquals(roleCount2, roleCount1 + 1);
        }
     
        @Test
        public void getAllEnvironmentsTest() throws Exception {
            this.mockMvc
                    .perform(get("/api/environments")
                            .accept(MediaType.APPLICATION_JSON)
                            .contentType(MediaType.APPLICATION_JSON)
                    )
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$", hasSize(1)))
                    .andExpect(jsonPath("$[0].name").value("DEV"))
                    .andReturn();
        }
     
        @Test
        public void searchAppNodesTest() throws Exception {
            this.mockMvc
                    .perform(get("/api/searchAppNodes")
                            .accept(MediaType.APPLICATION_JSON)
                            .contentType(MediaType.APPLICATION_JSON)
                            .param("search", "John Doe")
                    )
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$", hasSize(1)))
                    .andExpect(jsonPath("$[0].name").value("DEV"))
                    .andReturn();
        }
    }

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    septembre 2006
    Messages
    692
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : septembre 2006
    Messages : 692
    Points : 235
    Points
    235
    Par défaut
    J'utilise finalement junit5 et jupiter
    Pour debugger, j'ai modifié ma classe comme suit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @AutoConfigureMockMvc
    @Testcontainers
    @TestPropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application.properties")
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    public class ApplicationControllerTest  extends InserDataForIntegrationTests {
     
        @Autowired
        private MockMvc mockMvc;
     
        @Autowired
        private AuthenticationManager authManager;
     
        @Autowired
        private ObjectMapper objectMapper;
     
        @Autowired
        private WebApplicationContext context;
     
        @Container
        @Shared
        public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:latest")
                .withDatabaseName("staffaccess")
                .withUsername("sa")
                .withPassword("sa");
     
        static {
     
     
            System.setProperty("spring.datasource.driver-class-name", postgreSQLContainer.getDriverClassName());
            System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl());
            System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername());
            System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword());
        }
    J'ai une erreur à la ligne suivante
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl());
    l'erreur est

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
    Voici un extrait de mon fichier application.properties

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url=jdbc:tc:postgresql:latest:///staffaccess
    spring.jpa.hibernate.ddl-auto=create-drop

    UPDATED
    ------------
    Re,
    j'ai hardcodé le datasource.url

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @AutoConfigureMockMvc
    @Testcontainers
    @TestPropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application.properties")
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    public class ApplicationControllerTest  extends InserDataForIntegrationTests {
     
        @Autowired
        private MockMvc mockMvc;
     
        @Autowired
        private AuthenticationManager authManager;
     
        @Autowired
        private ObjectMapper objectMapper;
     
        @Autowired
        private WebApplicationContext context;
     
        @Container
        @Shared
        public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:latest")
                .withDatabaseName("staffaccess")
                .withUsername("sa")
                .withPassword("sa");
     
        @DynamicPropertySource
        static void postgresqlProperties(DynamicPropertyRegistry registry) {
            registry.add("spring.datasource.url", () -> "jdbc:tc:postgresql:latest:///staffaccess");
            registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
            registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
        }
    Et maintenant, j'ai l'erreur suivante:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
     
    java.lang.IllegalStateException: Failed to load ApplicationContext
     
    	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    	at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:350)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:355)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$7(ClassBasedTestDescriptor.java:350)
    	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    	at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
    	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    	at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
    	at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
    	at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:349)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$4(ClassBasedTestDescriptor.java:270)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:269)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:259)
    	at java.util.Optional.orElseGet(Optional.java:267)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:258)
    	at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$before$0(ClassBasedTestDescriptor.java:184)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:183)
    	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:136)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    	at java.util.ArrayList.forEach(ArrayList.java:1259)
    	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
    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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782)
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774)
    	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
    	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123)
    	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    	... 57 more
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    	... 74 more
    Caused by: org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
    	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    	at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69)
    	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.jdbcStatement(GenerationTargetToDatabase.java:77)
    	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:53)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:331)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:230)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126)
    	at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112)
    	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:145)
    	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318)
    	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
    	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259)
    	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    	... 78 more
    Caused by: java.sql.SQLException: Could not create new connection
    	at org.testcontainers.containers.JdbcDatabaseContainer.createConnection(JdbcDatabaseContainer.java:228)
    	at org.testcontainers.jdbc.ContainerDatabaseDriver.connect(ContainerDatabaseDriver.java:124)
    	at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
    	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
    	at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
    	at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
    	at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
    	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    	at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
    	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
    	at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43)

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    septembre 2006
    Messages
    692
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : septembre 2006
    Messages : 692
    Points : 235
    Points
    235
    Par défaut
    Bonjour, j'ai trouvé une solution. J'ai fait un pull d'une autre image de Postgres pour Docker: postgres:13.4-buster

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
     
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
            "spring.datasource.url=jdbc:tc:postgresql:13.4-buster:///databasename?TC_INITSCRIPT=file:src/test/resources/init_script.sql",
            "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
    })
    @AutoConfigureMockMvc
    @Testcontainers
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    public class ApplicationControllerTest  extends InserDataForIntegrationTests {
     
        @Autowired
        private MockMvc mockMvc;
     
        @Autowired
        private AuthenticationManager authManager;
     
        @Autowired
        private ObjectMapper objectMapper;
     
        @Autowired
        private WebApplicationContext context;
     
        @Container
        @Shared
        public static PostgreSQLContainer postgreSQLContainer  = (PostgreSQLContainer) new PostgreSQLContainer("postgres:13.4-buster")
                .withDatabaseName("databasename")
                .withUsername("sa")
                .withPassword("sa");;
     
     
        @DynamicPropertySource
        static void postgresqlProperties(DynamicPropertyRegistry registry) throws URISyntaxException {
            registry.add("spring.datasource.driver-class-name", () -> "org.testcontainers.jdbc.ContainerDatabaseDriver");
            registry.add("spring.datasource.url", () -> "jdbc:tc:postgresql:13.4-buster:///databasename?TC_INITSCRIPT=file:src/test/resources/init_script.sql");
            registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
            registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
        }
     
     
     
     
     
        @BeforeAll
        @Transactional
        public void setup() {
            postgreSQLContainer.start();
            mockMvc = MockMvcBuilders
                    .webAppContextSetup(context)
                    .apply(springSecurity())
                    .build();
            UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken("username", "password");
            Authentication auth = authManager.authenticate(authReq);
            SecurityContextHolder.getContext().setAuthentication(auth);
            insertData();
        }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Rien de plus que les tests d'intégration?
    Par rmaker dans le forum Qualimétrie
    Réponses: 7
    Dernier message: 01/02/2013, 12h59
  2. [Cobertura] [Maven] inclure les tests d'intégration dans les rapports de qualité
    Par Tcharl dans le forum Qualimétrie
    Réponses: 1
    Dernier message: 02/05/2011, 21h33
  3. Notifier quand les critères de recherche sont trouvés
    Par yacinechaouche dans le forum Général Conception Web
    Réponses: 4
    Dernier message: 10/11/2009, 20h45
  4. [Cobertura] [Maven] Instrumenter un WAR et analyser les tests d'intégration
    Par goldest dans le forum Qualimétrie
    Réponses: 2
    Dernier message: 27/04/2009, 18h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo