[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


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


: 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)


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


@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/


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) {
        return entity;

    public void flush() {

    public <E> E persistAndFlush(E entity) {
        return entity;

    public void detach(Object entity) {

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

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

Create a bean that returns TestEntityManager (starting from 1.4)


public class TestEntityManagerConfigure {

    public TestEntityManager testEntityManager(
            EntityManagerFactory entityManagerFactory) {
        return new TestEntityManager(entityManagerFactory);


Add @Transactional and @SpringApplicationConfiguration to the test class


import org.springframework.transaction.annotation.Transactional;

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

    private MySomeRepository instance;

    private TestEntityManager em;

    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


    private MyTargetClass mock;

For 1.2

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


public class MySomeTest {
    private MyTargetClass mock;


public class MySomeTestConfig {
    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