在Spring Boot中使用@TestConfiguration时,bean总是无效的

发布于 2025-02-03 04:53:43 字数 1807 浏览 1 评论 0原文

我在应用程序中使用一个简单的testFiguration调用来初始化测试类中的服务bean:

    @TestConfiguration
    static class UserServiceConfiguration {
        @Bean
        public UserService userService() {
            return new UserServiceImpl() {

            };
        }
    }

    @Autowired
    private UserService userService;

当我尝试在类中运行任何测试时,我立即得到一个“ this.userService为null is null”错误,而测试则会失败。指针异常。

我尝试了一些不同的修复程序,包括:

  • 将testConfiguration放在自己的类中,并通过@Import注释将其
  • 导入到bean中 @primary,并在@bean注释中给它一个唯一的名称。

未调用testConfiguration返回中的断点,因此看起来好像不是自动连接到该版本的服务。

请注意,我是Spring Boot中的单元测试的新手,我们的应用程序已有几年的历史,并在不同的地方进行了各种配置。我想知道还有其他事情正在干扰测试配置,但是我的理解是,它应该覆盖服务并忽略这些配置吗?我只是无法弄清楚为什么没有被调用。

我使用的是Spring Boot 2.5.4,Java18。

完整的测试类别代码:


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;

import com.evotext.user.service.UserService;
import com.evotext.user.service.UserServiceImpl;

@RunWith(SpringRunner.class)
public class UserServiceTest {

    @TestConfiguration
    static class UserServiceConfiguration {
        @Bean
        public UserService userService() {
            return new UserServiceImpl() {

            };
        }
    }

    @Autowired
    private UserService userService;
    
    
    
    @Test
    public void getCurrentUserSession() {
        
        long l = userService.countTotalUsers();
        assertThat(l == 1);
    }
    
}

服务类本身是接口,并且已用@Service和 @transactional注释了实现的版本,未指定名称。

I am using a simple TestConfiguration call in my application to initialize a service bean within the test class:

    @TestConfiguration
    static class UserServiceConfiguration {
        @Bean
        public UserService userService() {
            return new UserServiceImpl() {

            };
        }
    }

    @Autowired
    private UserService userService;

When I try running any tests within the class, I immediately get a "this.userService is null" error and the test fails out with a null pointer exception.

I've tried some different fixes, including:

  • Putting the TestConfiguration in its own class and importing it via the @Import annotation
  • Adding @Primary to the bean as well as giving it a unique name in the @Bean annotation.

A breakpoint in the TestConfiguration return is not called, so it appears as if the service is not being automatically wired to that version.

Note I am new to unit testing in Spring boot and our application is a few years old with various configurations in different places. I am wondering if something else is interfering with the TestConfiguration, but my understanding was that it was supposed to override the service and ignore those configurations? I just cannot figure out why it is not being called.

I am using Spring Boot 2.5.4, Java 18.

Complete code of Test Class:


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;

import com.evotext.user.service.UserService;
import com.evotext.user.service.UserServiceImpl;

@RunWith(SpringRunner.class)
public class UserServiceTest {

    @TestConfiguration
    static class UserServiceConfiguration {
        @Bean
        public UserService userService() {
            return new UserServiceImpl() {

            };
        }
    }

    @Autowired
    private UserService userService;
    
    
    
    @Test
    public void getCurrentUserSession() {
        
        long l = userService.countTotalUsers();
        assertThat(l == 1);
    }
    
}

The service class itself is an interface and the implemented version is annotated with @Service and @Transactional, no name is specified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2025-02-10 04:53:43

我无法使它起作用,但是由于最终目标是进行一项没有带来完整春季配置的测试,所以我确实找到了工作:

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {

    @Mock
    private UserDao userDao;
    
    @InjectMocks
    private UserServiceImpl userService;

   //Your tests and what not
}

查看此教程提供了更好的详细说明。

I was unable to get this to work, but since the end goal was to make a test that didn't bring in my full Spring configuration, I did find a work around:

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {

    @Mock
    private UserDao userDao;
    
    @InjectMocks
    private UserServiceImpl userService;

   //Your tests and what not
}

Check out this tutorial for a better detailed explanation.

著墨染雨君画夕 2025-02-10 04:53:43

使用org.junit.test,不是org.junit.jupiter.api.test。

import org.junit.Test;
//import org.junit.jupiter.api.Test;

use org.junit.Test, NOT org.junit.jupiter.api.Test.

import org.junit.Test;
//import org.junit.jupiter.api.Test;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文