Springboot uses version 2.1.4
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
//@ContextConfiguration(value = {"classpath*:application.yml","classpath*:logback-spring.xml"})
public class LoggerTest {
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
@Test
public void test(){
String name="zhangsan";
String password = "123";
logger.debug("ggggg");
logger.error("ddddd");
logger.info("ffff");
logger.info("name: {},password: {}",name,password);
}
}
During unit testing, springboot always reports an error when starting
Failed to load ApplicationContext
So all kinds of search data are basically due to the need to add the annotation:
(1)@ContextConfiguration(locations= {“classpath*:application.yml”,”classpath*:logback-spring.xml”})
(2) Or the configuration file in the annotation is not added completely
(3) Or the path of the annotation configuration file is wrong.
My profile path is as follows:
Only these two, the path is right, but has been throwing red report error.
Later, I thought that there might be other reasons, so I annotated the annotation and retested it. It was found that although the error was still failed to load ApplicationContext, the information of caused by error was as follows:
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
……..
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
The error message should be related to the hibernate.dialect configuration of the configuration file, so try to modify the configuration file:
Add database platform in spring.datasource.jpa: org.hibernate.dialect.mysql5innodbdialect
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/selling?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
jackson:
default-property-inclusion: non_null
redis:
host: 127.0.0.1
port: 6379
Run again, success!
When testing another method, an error is reported:
Table ‘selling.hibernate_ sequence’ doesn’t exist
This kind of primary key is set to self growth, plus the following strategies:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;