英文:
When Junit Test with Spring throws Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j
问题
My service is running fine, but when I write a test it gives the error I specified.
我的服务运行正常,但当我编写测试时,它出现了我指定的错误。
I tried several methods but couldn't find the problem.
我尝试了几种方法,但找不到问题。
dependency
依赖
<spring-boot.version>2.7.4</spring-boot.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
My Business class
我的业务类
public class UserBusinessService {
static Logger logger = LogFactory.getLogger(UserBusinessService.class);
private final UserRepository userRepository;
public UserBusinessService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void deleteUser(Long id) {
userRepository.deleteUserByUserId(id);
logger.info("deleted");
}
}
My Test Class
我的测试类
@ExtendWith(MockitoExtension.class)
class UserBusinessServiceTest {
@Mock
UserRepository userRepository;
@InjectMocks
UserBusinessService userBusinessService;
@Test
void deleteUser_itShouldReturnStatusNotFound_whenUserNotExist() throws Exception {
// given
when(userRepository.deleteUserByUserId(anyLong()));
// when
Executable executable = () -> userBusinessService.deleteUser(1L);
// then
assertThrows(BusinessException.class, executable);
}
}
How can I fix this problem?
我该如何修复这个问题?
英文:
My service is running fine, but when I write a test it gives the error I specified.
I tried several methods but couldn't find the problem.
dependency
<spring-boot.version>2.7.4</spring-boot.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
My Bussiness class
public class UserBusinessService {
static Logger logger = LogFactory.getLogger(UserBusinessService.class);
private final UserRepository userRepository;
public UserBusinessService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void deleteUser(Long id) {
userRepository.deleteUserByUserId(id);
logger.info("deleted");
}
}
MY Test Class
@ExtendWith(MockitoExtension.class)
class UserBusinessServiceTest {
@Mock
UserRepository userRepository;
@InjectMocks
UserBusinessService userBusinessService;
@Test
void deleteUser_itShouldReturnStatusNotFound_whenUserNotExist() throws Exception {
// given
when(userRepository.deleteUserByUserId(anyLong()));
// when
Executable executable = () -> userBusinessService.deleteUser(1L);
// then
assertThrows(BusinessException.class, executable);
}
}
how can i fix this problem?
答案1
得分: 0
这个问题是通过排除依赖项来解决的,如下所示。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
英文:
This problem was fixed by exclusion like this.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论