Java 如何使用SpringBoot正确测试存储库层

Java 如何使用SpringBoot正确测试存储库层,java,spring,spring-boot,junit,Java,Spring,Spring Boot,Junit,首先,我是项目新手,我有一项任务要测试存储库层。 我从创建测试类开始,并试图正确地注释他。 我们有相当大的数据库,所以我尝试添加新属性,以便在内存数据库中检查它。 我跟着贝尔东 我有下一个application-unit-tests-h2db.properties: #Database spring.datasource.url = jdbc:h2:mem:test spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H

首先,我是项目新手,我有一项任务要测试存储库层。 我从创建测试类开始,并试图正确地注释他。 我们有相当大的数据库,所以我尝试添加新属性,以便在内存数据库中检查它。 我跟着贝尔东

我有下一个application-unit-tests-h2db.properties:

#Database
spring.datasource.url = jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop
城市报告测试:

public class CityRepositoryTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private CityRepository cityRepository;

    @Test
    public void contextLoads() {
        Assert.assertNotNull(cityRepository);
        Assert.assertNotNull(mvc);
    }
}
我在尝试运行测试时遇到下一个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:test
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:311) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755) ~[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402) [spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) [spring-boot-test-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43) [spring-boot-test-autoconfigure-2.3.10.RELEASE.jar:2.3.10.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.13.2.jar:4.13.2]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) [junit-4.13.2.jar:4.13.2]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) [junit-4.13.2.jar:4.13.2]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) [junit-4.13.2.jar:4.13.2]
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) [junit-4.13.2.jar:4.13.2]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) [junit-4.13.2.jar:4.13.2]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) [junit-4.13.2.jar:4.13.2]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413) [junit-4.13.2.jar:4.13.2]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.13.2.jar:4.13.2]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) [junit-rt.jar:?]
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) [junit-rt.jar:?]
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220) [junit-rt.jar:?]
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53) [junit-rt.jar:?]
Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:test
    at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-3.4.5.jar:?]
    at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:325) ~[HikariCP-3.4.5.jar:?]
    at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[HikariCP-3.4.5.jar:?]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108) ~[HikariCP-3.4.5.jar:?]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar:?]
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:272) ~[liquibase-core-4.3.2.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1858) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1795) ~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
org.springframework.beans.factory.BeanCreationException:创建名为“liquibase”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/liquibase/liquibseautoconfiguration$liquibseconfiguration.class]:调用init方法失败;嵌套异常是java.lang.RuntimeException:Driver org.postgresql.Driver声明不接受jdbcUrl,jdbc:h2:mem:test
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:311)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)~[spring-beans-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109)~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)~[spring-context-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)~[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755)~[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
位于org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402)[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:312)[spring-boot-2.3.10.RELEASE.jar:2.3.10.RELEASE]
位于org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)[spring-boot-test-2.3.10.RELEASE.jar:2.3.10.RELEASE]
在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
位于org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
位于org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)[spring-boot-test-autoconfigure-2.3.10.RELEASE.jar:2.3.10.RELEASE]
在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectCall(SpringJUnit4ClassRunner.java:289)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
在org.junit.internal.runners.model.ReflectVeCallable.run(reflectVeCallable.java:12)[junit-4.13.2.jar:4.13.2]
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)[spring-test-5.2.14.RELEASE.jar:5.2.14.RELEASE]
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("unit-tests")
public class CityRepositoryTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private CityRepository cityRepository;

    @Test
    public void contextLoads() {
        Assert.assertNotNull(cityRepository);
        Assert.assertNotNull(mvc);
    }

    @Before
    public void setup() {
        City city1 = City.builder().id(1L).name("SAMARA").build();
        City city2 = City.builder().id(2L).name("SAINT-PETERSBURG").build();
        City city3 = City.builder().id(2L).name("TULA").build();
        City city4 = City.builder().id(2L).name("SARANSK").build();
        City city5 = City.builder().id(2L).name("SYZRAN").build();

        cityRepository.save(city1);
        cityRepository.save(city2);
        cityRepository.save(city3);
        cityRepository.save(city4);
        cityRepository.save(city5);

    }

    @Test
    public void findCity() {
        String name = "SARANSK";

        Optional<City> optionalCity = cityRepository.findByNameIgnoreCase(name);
        City city1 = optionalCity.isPresent() ? optionalCity.get() : new City();

        System.out.println(city1);

        Assert.assertEquals("SARANSK", city1.getName());
    }
}
    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  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>

  <artifactId>helpering</artifactId>
  <groupId>ru.helpering</groupId>
  <name>HelperingBackend</name>
  <description>Helpering Backend API</description>
  <version>0.2.4</version>
  <packaging>jar</packaging>

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

  <properties>
    <java.version>14</java.version>
    <maven.compiler.release>14</maven.compiler.release>
    <maven.compiler.target>14</maven.compiler.target>
    <maven.compiler.source>14</maven.compiler.source>
    <!--    <maven.deploy.skip>true</maven.deploy.skip>-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <start-class>ru.helpering.Application</start-class>
    <aws-java-sdk-s3.version>1.11.862</aws-java-sdk-s3.version>
    <hibernate-jpamodelgen.version>5.4.21.Final</hibernate-jpamodelgen.version>
    <jsonwebtoken.version>0.9.1</jsonwebtoken.version>
    <liquibase.version>4.3.2</liquibase.version>
    <com.google.code.gson.version>2.8.6</com.google.code.gson.version>
    <!--    <swagger-annotations.version>1.5.21</swagger-annotations.version>-->
    <!--    <swagger-models.version>1.5.21</swagger-models.version>-->
    <swagger.version>2.10.5</swagger.version>
  </properties>

  <dependencies>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>logback-classic</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-messaging</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

    <!-- Database -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- liquibase -->
    <dependency>
      <groupId>org.liquibase</groupId>
      <artifactId>liquibase-core</artifactId>
      <version>${liquibase.version}</version>
    </dependency>
    <dependency>
      <groupId>org.liquibase</groupId>
      <artifactId>liquibase-groovy-dsl</artifactId>
      <version>3.0.0</version>
    </dependency>

    <!-- Development -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>

    <!-- Logger -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <!-- Lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>

    <!-- swagger -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${swagger.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${swagger.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-data-rest</artifactId>
      <version>${swagger.version}</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-bean-validators</artifactId>
      <version>${swagger.version}</version>
    </dependency>

    <!-- Token & JWT -->
    <dependency>
      <groupId>io.jsonwebtoken</groupId>
      <artifactId>jjwt</artifactId>
      <version>${jsonwebtoken.version}</version>
    </dependency>

    <!-- Генератор метамодели классов-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <version>${hibernate-jpamodelgen.version}</version>
    </dependency>

    <!-- JSON -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>${com.google.code.gson.version}</version>
    </dependency>

    <!-- Utils -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.10</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.8.0</version>
    </dependency>

    <!-- AWS -->
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>${aws-java-sdk-s3.version}</version>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <artifactId>rest-assured</artifactId>
      <groupId>io.rest-assured</groupId>
      <scope>test</scope>
      <version>4.3.0</version>
    </dependency>
    <dependency>
      <artifactId>json-path</artifactId>
      <groupId>io.rest-assured</groupId>
      <version>4.3.0</version>
    </dependency>
    <dependency>
      <artifactId>xml-path</artifactId>
      <groupId>io.rest-assured</groupId>
      <version>4.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>local</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <spring.profiles.active>local</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>local-dll-auto</id>
      <properties>
        <spring.profiles.active>local-dll-auto</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>dev</id>
      <properties>
        <spring.profiles.active>dev</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <spring.profiles.active>prod</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>unit-tests</id>
      <properties>
        <spring.profiles.active>unit-tests</spring.profiles.active>
      </properties>
    </profile>
  </profiles>

  <build>
    <finalName>${project.name}</finalName>
    <plugins>
      <plugin>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <groupId>org.springframework.boot</groupId>
      </plugin>
      <!--      <plugin>-->
      <!--        <groupId>org.apache.maven.plugins</groupId>-->
      <!--        <artifactId>maven-compiler-plugin</artifactId>-->
      <!--        <configuration>-->
      <!--          <source>14</source>-->
      <!--          <target>14</target>-->
      <!--          <release>14</release>-->
      <!--          <compilerArgs>&#45;&#45;enable-preview-->
      <!--          </compilerArgs>-->
      <!--          <forceJavacCompilerUse>true</forceJavacCompilerUse>-->
      <!--          <parameters>true</parameters>-->
      <!--        </configuration>-->
      <!--      </plugin>-->
      <!--HEROKU DEPLOY-->
      <!--      &lt;!&ndash;https://devcenter.heroku.com/articles/deploying-java-applications-with-the-heroku-maven-plugin&ndash;&gt;-->
      <!--      <plugin>-->
      <!--        <groupId>com.heroku.sdk</groupId>-->
      <!--        <artifactId>heroku-maven-plugin</artifactId>-->
      <!--        <version>3.0.4</version>-->
      <!--        <configuration>-->
      <!--          <appName>${project.artifactId}</appName>-->
      <!--          <includeTarget>false</includeTarget>-->
      <!--          <includes>-->
      <!--            <include>${project.build.directory}/${project.build.finalName}.jar</include>-->
<!--          </includes>-->
<!--          <jdkVersion>14</jdkVersion>-->
<!--          <processTypes>-->
<!--            <web>java $JAVA_OPTS -Dserver.port=$PORT -Dspring.profiles.active=dev -jar ${project.build.directory}/${project.build.finalName}.jar</web>-->
<!--          </processTypes>-->
<!--        </configuration>-->
<!--      </plugin>-->
      <!--HEROKU DOCKER-->
<!--      <plugin>-->
<!--        <groupId>com.spotify</groupId>-->
<!--        <artifactId>dockerfile-maven-plugin</artifactId>-->
<!--        <version>1.4.13</version>-->
<!--        <executions>-->
<!--          <execution>-->
<!--            <id>default</id>-->
<!--            <goals>-->
<!--              <goal>build</goal>-->
<!--              <goal>push</goal>-->
<!--            </goals>-->
<!--          </execution>-->
<!--        </executions>-->
<!--        <configuration>-->
<!--          <repository>registry.heroku.com/${project.artifactId}/web</repository>-->
<!--          <tag>latest</tag>-->
<!--          <buildArgs>-->
<!--            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>-->
<!--          </buildArgs>-->
<!--        </configuration>-->
<!--      </plugin>-->
    </plugins>
  </build>

</project>
package so67593864;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration;
import so67593864.CityRepositoryTest.TestConfig;

@DataJpaTest
@ContextConfiguration(classes = TestConfig.class)
public class CityRepositoryTest {

    @Autowired
    private CityRepository cityRepository;

    @Test
    public void contextLoads() {
        assertThat(cityRepository).isNotNull();
    }

    @BeforeEach
    public void setup() {
        City city1 = City.builder().id(1L).name("SAMARA").build();
        City city2 = City.builder().id(2L).name("SAINT-PETERSBURG").build();
        City city3 = City.builder().id(2L).name("TULA").build();
        City city4 = City.builder().id(3L).name("SARANSK").build();
        City city5 = City.builder().id(2L).name("SYZRAN").build();

        cityRepository.save(city1);
        cityRepository.save(city2);
        cityRepository.save(city3);
        cityRepository.save(city4);
        cityRepository.save(city5);

    }

    @Test
    public void findCity() {
        String name = "SARANSK";

        Optional<City> optionalCity = cityRepository.findByNameIgnoreCase(name);

        City city1 = optionalCity.isPresent() ? optionalCity.get() : new City();

        System.out.println(city1);

        assertThat("SARANSK").isEqualTo(city1.getName());
    }

    @Configuration
    @EnableJpaRepositories(basePackageClasses = CityRepository.class)
    @EntityScan(basePackageClasses = City.class)
    static class TestConfig {

    }
}