X
X
xxxxc2018-03-24 00:31:56
Spring
xxxxc, 2018-03-24 00:31:56

The class for testing in Spring Boot loads only its automatic configuration, how to load your own?

Help me to understand. I use Spring Boot 2.0 with Spring Data JPA
There is a main class

@SpringBootApplication
public class PhoneBookApplication {
    public static void main(String[] args) {
        SpringApplication.run(PhoneBookApplication.class, args);
    }
}

Also created the most basic repository:
public interface UserRepository extends JpaRepository<User, Integer>{}

There is an ordinary entity, with all the annotations, getters, settars and a constructor.
Now I'm trying to test the work with the database. Created a simple test.
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes=PhoneBookApplication.class)    
@DataJpaTest
public class UserRepositoryTest {
  @Autowired
  public UserRepository userRepository;
  @Test 
  public void createUser(){
    User u = new User();
    u.setFullName("FullName");
    u.setPassword("123456");
    u.setUserName("Name");
    userRepository.save(u);
  }

And this is where all the problems happen. Due to the use of the DataJpaTest annotation - Spring automatically only works with h2. I have application.properties in which i tried to use mySql. But spring boot still loads only its own. I found an annotation to change the database, but somehow it doesn't work @AutoConfigureTestDatabase(replace=Replace.NONE)
I've already decided that I won't be able to use mySql for tests. Is it also a bad idea to use an embedded database for tests? But despite this, I want to specify some configs in application.properties already for h2. But the spring does not load them, even created profiles and still 0 effect.
How I use profiles: in @ActiveProfiles("test") test
In application.properties even hardcore for spring.profiles.active tests: test
Well, the properties themselves for h2 in the application-test.properties file

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xxxxc, 2018-03-24
@xxxxc

Even explicitly pointing to the config via
@PropertySource("classpath:application.properties")
does not load

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question