[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note

I don't think anyone would do this, but I've suffered so much so I'll make a note of it.

About the notation of this sentence

: grin: 1.4: How to do it in case of 1.4. : smirk: 1.2: 1.2 is how to do it.

Implementation relationship

@EntityScan

:grin: 1.4: org.springframework.boot.autoconfigure.domain.EntityScan :smirk: 1.2: org.springframework.boot.orm.jpa.EntityScan

DI

: grin: 1.4: If it is self-evident, DI will be done without adding @Autowired. : smirk: 1.2: Let's add @Autowired.

Test related

Test runner

:grin: 1.4: @RunWith(SpringRunner.class) :smirk: 1.2: @RunWith(SpringJUnit4ClassRunner.class)

Testing with Spring

: grin: 1.4: Add @SpringBootTest : smirk: 1.2: Add @SpringApplicationConfiguration (classes = application classes)

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/SpringApplicationConfiguration.html

Database integration test

: grin: 1.4: Add @DataJpaTest to the test class. : smirk: 1.2: It's hard.

@DataJpaTest seems to do this.

  1. Set up in-memory database
  2. Set Hibernate / Spring Data / Datasource
  3. Execute @EntityScan
  4. Enable SQL logging

I thought I had to do everything, but it seems that it's okay if I do all this.

  1. Create an application context class with @EntityScan and @EnableJpaRepositories.
  2. Create a TestEntityManager class (starting from 1.4)
  3. Create a bean that returns TestEntityManager (starting from 1.4)
  4. Add @Transactional and @SpringApplicationConfiguration to the test class.

I will do it in order.

Create application context class with @EntityScan, @EnableJpaRepositories

TestApplication.java


@SpringBootApplication
@EnableJpaRepositories(Location of repository class)
@EntityScan(Entity class location)
@EnableJpaAuditing //This is irrelevant this time
public class TestApplication {

}

Create TestEntityManager class (starting from 1.4)

The original source is here. https://github.com/spring-projects/spring-boot/blob/master/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/

TestEntityManager.java


public class TestEntityManager {

    private final EntityManagerFactory entityManagerFactory;

    public TestEntityManager(EntityManagerFactory entityManagerFactory) {
        Assert.notNull(entityManagerFactory, "EntityManagerFactory must not be null");
        this.entityManagerFactory = entityManagerFactory;
    }

    public <E> E persist(E entity) {
        getEntityManager().persist(entity);
        return entity;
    }

    public void flush() {
        getEntityManager().flush();
    }

    public <E> E persistAndFlush(E entity) {
        persist(entity);
        flush();
        return entity;
    }

    public void detach(Object entity) {
        getEntityManager().detach(entity);
    }

    public <E> E find(Class<E> entityClass, Object primaryKey) {
        return getEntityManager().find(entityClass, primaryKey);
    }

    public final EntityManager getEntityManager() {
        EntityManager manager = EntityManagerFactoryUtils
                .getTransactionalEntityManager(this.entityManagerFactory);
        Assert.state(manager != null, "No transactional EntityManager found");
        return manager;
    }
}

Create a bean that returns TestEntityManager (starting from 1.4)

TestEntityManagerConfigure.java


@Configuration
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
public class TestEntityManagerConfigure {

    @Bean
    @ConditionalOnMissingBean
    public TestEntityManager testEntityManager(
            EntityManagerFactory entityManagerFactory) {
        return new TestEntityManager(entityManagerFactory);
    }

}

Add @Transactional and @SpringApplicationConfiguration to the test class

MySomeRepositoryTest.java


import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
@Transactional
public class MySomeRepositoryTest {

    @Autowired
    private MySomeRepository instance;

    @Autowired
    private TestEntityManager em;

    @Test
    public void testSomeMethod {
     // ...
    }

}


Inject a mocked instance

: grin: 1.4: Add @MockBean to the instance variable you want to mock : smirk: 1.2: Define a @Bean that returns a mocked instance and add @Primary

For 1.4

MySomeTest.java


    @MockBean
    private MyTargetClass mock;

For 1.2

@Profile is added so that only a specific @Configuration class is valid.

MySomeTest.java


@ActiveProfiles("MySomeTest")
public class MySomeTest {
    @Autowired
    private MyTargetClass mock;
}

MySomeTestConfig.java


@Configuration
@Profile("MySomeTest")
public class MySomeTestConfig {
    @Bean
    @Primary
    public MyTargetClass myTargetClass() {
        return Mockito.mock(MyTargetClass.class);
    }
}

The journey continues. .. ..

If you find something, I will add it.

Recommended Posts

[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
[Java] Thymeleaf Basic (Spring Boot)
CICS-Run Java application-(4) Spring Boot application
Spring Java
Spring Boot + Java + GitHub authentication login
Elastic Beanstalk (Java) + Spring Boot + https
Java --Jersey Framework vs Spring Boot
[Java] LINE integration with Spring Boot
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
Challenge Spring Boot
Create Java Spring Boot project in IntelliJ
Spring Boot + Docker Java development environment construction
Downgrade Java MacOS
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
[Java] Spring DI ③
[Java / Spring Boot] Spring security ④ --Implementation of login process
Java tips-Create a Spring Boot project in Gradle
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[Java] Article to add validation with Spring Boot 2.3.1.
LINE Bot x Java (Spring Boot) construction procedure
Java abstract modifier [Note]
[Java] Internal Iterator Note
Spring boot memo writing (1)
Spring Cloud Netflix Note
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
[java] Java SE 8 Silver Note
Spring Boot Servlet mapping
Spring boot development-development environment-
[Note] Java: String search
[Note] Java: String survey
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
My Study Note (Java)
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
java: Add date [Note]
Create a portfolio app using Java and Spring Boot
[Note] Configuration file when using Logback with Spring Boot
Please note that Spring Boot + Tomcat 8.5.8 cannot be used!
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
A note about Java GC
[Spring Boot] Environment construction (macOS)
[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Downgrade Java on openSUSE Linux
Spring + Gradle + Java Quick Start
Handle Java 8 date and time API with Thymeleaf with Spring Boot
(Note) Java classes / variables / methods