When Junit Test with Spring throws Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j

huangapple go评论68阅读模式
英文:

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

&lt;spring-boot.version&gt;2.7.4&lt;/spring-boot.version&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;/dependency&gt;

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(&quot;deleted&quot;);
    }
}

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 = () -&gt; 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.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
            &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
        &lt;/exclusion&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j-to-slf4j&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年3月3日 19:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定