Spring Boot @RestController @autowired Null在单元测试中

发布于 2025-01-19 07:18:14 字数 1807 浏览 0 评论 0原文

我不明白为什么 @Autowiring 我的 @RestController 类返回 null。 我想在进行集成测试之前进行基本的单元测试,但失败了。 事实上,@Autowired 的任何内容在测试包中都显示为 null。

我有一个非常简单的测试,我只想看看基本的工作:

一个非常简单的例子:

@Component
public class SimpleComponent {

    public int add(int a, int b){
        return a + b;
    }

}

和测试:

class SimpleComponentTest {

    @Autowired
    private SimpleComponent component;

    @Test
    public void givenSimpleComponentShouldNotBeNull(){
        assertNotNull(component);//How on earth does this fail?
    }


}

这是来自 Controller 类的代码:

@RestController
public class EmployeeAccountApi {

    @PostMapping("/register")
    public String register(){
        return "Registering!";
    }

}

public class EmployeeAccountApiUnitTest {

    @Autowired
    private EmployeeAccountApi employeeAccountApi;

    @Test
    public void testAutowiring(){
        assertNotNull(employeeAccountApi);//Fails, Can't understand why
    }

}

这与 @Mock 注释一起使用: 但我不想嘲笑它,因为那是我正在单元测试的类。 我想测试该方法是否返回一个基本字符串。 问题是为什么它不起作用?

@ExtendWith(MockitoExtension.class)
public class EmployeeAccountApiUnitTest {

    @Mock
    private EmployeeAccountApi employeeAccountApi;

    @Test
    public void testAutowiring(){
        assertNotNull(employeeAccountApi);//This works
    }

}

即使我得到在之前的测试中明确工作和测试的 Employee Service 在此类中也为 null:

public class EmployeeAccountApiUnitTest {

    @Autowired
    private EmployeeAccountApi employeeAccountApi;

    @Autowired
    private EmployeeService service;

    @Test
    public void testAutowiring(){
        //assertNotNull(employeeAccountApi);
        assertNotNull(service);
    }

}

为什么 @Autowired 在测试中为 null? 模拟工作正常

有很多类似的问题,但它们太具体并且没有提供任何基本信息

I can't understand why @Autowiring my @RestController class is returning null.
I want to do a basic unit test before doing an integrated test but its failing.
In fact anything that is being @Autowired is showing null in the test package.

I have a very simple test, I just want to see the basic works:

A very simple example:

@Component
public class SimpleComponent {

    public int add(int a, int b){
        return a + b;
    }

}

And the test:

class SimpleComponentTest {

    @Autowired
    private SimpleComponent component;

    @Test
    public void givenSimpleComponentShouldNotBeNull(){
        assertNotNull(component);//How on earth does this fail?
    }


}

Here is the code from the Controller class:

@RestController
public class EmployeeAccountApi {

    @PostMapping("/register")
    public String register(){
        return "Registering!";
    }

}

public class EmployeeAccountApiUnitTest {

    @Autowired
    private EmployeeAccountApi employeeAccountApi;

    @Test
    public void testAutowiring(){
        assertNotNull(employeeAccountApi);//Fails, Can't understand why
    }

}

This works with the @Mock annotation:
But I dont want to mock it as that is the class I am unit testing.
I want to test that the method returns a basic string.
Question is why isn't it working?

@ExtendWith(MockitoExtension.class)
public class EmployeeAccountApiUnitTest {

    @Mock
    private EmployeeAccountApi employeeAccountApi;

    @Test
    public void testAutowiring(){
        assertNotNull(employeeAccountApi);//This works
    }

}

Even if I get the Employee Service that clearly worked and tested in the previous test also is null in this class:

public class EmployeeAccountApiUnitTest {

    @Autowired
    private EmployeeAccountApi employeeAccountApi;

    @Autowired
    private EmployeeService service;

    @Test
    public void testAutowiring(){
        //assertNotNull(employeeAccountApi);
        assertNotNull(service);
    }

}

Why is @Autowired null in the Test?
Mocking is working fine

Plenty of similar questions but they are too specific and not provide any basic information

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

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

发布评论

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

评论(2

明天过后 2025-01-26 07:18:14

@Autowired仅在您为应用程序的应用程序上下文旋转时才有效。用@SpringBoottest注释测试类,该类别告诉测试开始整个Spring应用程序上下文,并且应该有效。

@Autowired only works if you are spinning up the application context for your application. Annotate the test class with @SpringBootTest, which tells the test to start the entire spring application context, and it should work.

怎樣才叫好 2025-01-26 07:18:14

@SpringBootTest 必须在 Spring 的测试类上使用,以加载您的 ApplicationContext 并进行连接。如果没有这个,Spring 就无法注入任何东西。

您可以使用 @WebMvcTest 在测试类上,将测试仅集中在 Web 层上。

有一大堆注释允许您以许多其他方式“分割”您的 ApplicationContext 来测试应用程序的各个其他部分。

Spring的文档和教程确实非常好。你应该检查一下。

测试 Web 层

@SpringBootTest has to be used on the test class for Spring to load your ApplicationContext and wire things up. Without out this there's nothing for Spring to inject.

You can use @WebMvcTest on the test class to focus your tests only on the Web Layer.

There's a whole bunch of annotations that allow you to "slice" your ApplicationContext up lots of other ways for testing various other parts of your application.

Spring's documentation and tutorials are really very good. You should check them out.

Testing the Web Layer

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