使用Unittests JDBC连接打包Spring Boot应用程序

时间:2017-06-10 10:36:34

标签: spring-boot spring-boot-maven-plugin

我正在尝试打包Spring Boot应用程序,其中有一些单元测试。在环顾四周寻找答案后,我想也许我会得到(可能很明显的)答案。

运行的命令是mvn package

我得到的错误是: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections

2017-06-10 12:10:12.156  INFO 9822 --- [main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2017-06-10 12:10:12.158  WARN 9822 --- [main] o.h.j.i.EntityManagerFactoryRegistry     : HHH000436: Entity manager factory name (default) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
2017-06-10 12:10:12.158  INFO 9822 --- [main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-10 12:10:12.197  INFO 9822 --- [main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2017-06-10 12:10:12.223  INFO 9822 --- [main] o.s.ws.soap.saaj.SaajSoapMessageFactory  : Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
2017-06-10 12:10:12.359  INFO 9822 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

...

Tests in error: 
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  SessionServiceTest.setUp:36 » CannotCreateTransaction Could not open JPA Entit...
  IBANGeneratorTest.generateIban:50 » DataAccessResourceFailure Unable to acquir...

...

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 53.643 s
[INFO] Finished at: 2017-06-10T12:10:18+02:00
[INFO] Final Memory: 20M/335M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project bank: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/johan/Devel/internetbankieren/bank/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我会说我有一个非常简单的设置。

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/bank?useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=bank
spring.datasource.password=************

spring.datasource.max-active=5
OR
spring.datasource.tomcat.max-active

spring.datasource.pool-size=20
OR
spring.datasource.tomcat.pool=20

spring.jpa.database_platform=org.hibernate.dialect.MySQL5Dialect

spring.jpa.hibernate.ddl-auto=create

spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false

spring.datasource.testWhileIdle=true
spring.datasource.timeBetweenEvictionRunsMillis=3600000
spring.datasource.validationQuery=SELECT 1

server.port=8082

的AppConfig

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan({"com.flojo.internetbankieren"})
@PropertySource("classpath:application.properties")
@EnableConfigurationProperties(BankProperties.class)
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

单元测试

import com.flojo.internetbankieren.config.AppConfig;
import com.flojo.internetbankieren.domain.BankAccount;
import com.flojo.internetbankieren.domain.Customer;
import com.flojo.internetbankieren.domain.Session;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class BankAccountServiceTest {
    private BankAccountService bankAccountService;
    private SessionService sessionService;
    private CustomerService customerService;    

    private String ip = "123.123.132.123";

    @Before
    public void setUp() throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        this.customerService = (CustomerService) context.getBean("customerService");
        this.sessionService = (SessionService) context.getBean("sessionService");
        this.bankAccountService = (BankAccountService) context.getBean("bankAccountService");
    }

    @Test
    public void createAccount() throws Exception {

        Customer customer = this.customerService.register("Johan", "Helmond", "1234");
        Session session = this.sessionService.startSession(ip, "johanhelmond", "1234");

        BankAccount account = this.bankAccountService.createAccount(session);
        assertNotNull(account);
        assertEquals(1, this.bankAccountService.getCount());
    }
}

MySql max_connections

mysql> show variables like "max_connections";
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0,01 sec)

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.flojo.internetbankieren</groupId>
    <artifactId>bank</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.3.RELEASE</version>
            </plugin>

            <!-- SOAP Client -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>com.flojo.internetbankieren.wsdl</generatePackage>
                    <!--<schemas>-->
                        <!--<schema>-->
                            <!--&lt;!&ndash; TODO Set the correct URL to the central bank &ndash;&gt;-->
                            <!--<url>http://www.webservicex.com/stockquote.asmx?WSDL</url>-->
                        <!--</schema>-->
                    <!--</schemas>-->
                </configuration>
            </plugin>

            <!-- Soap server -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我真的希望有人可以帮助我。

0 个答案:

没有答案