gpt4 book ai didi

spring - 在 JUnit 测试类中使用 @Autowired 会抛出 NullPointerException?

转载 作者:行者123 更新时间:2023-12-02 20:37:55 27 4
gpt4 key购买 nike

我正在使用 Autowiring (@Autowired) 在 JUnit 测试类中注入(inject)依赖项并面临 NullPointerException。我想知道是否可以使用/在 JUnit 测试类中进行 Autowiring 。否则应该如何在测试类中注入(inject) bean。我的代码如下 -

主类/客户端 - Autowiring 按预期工作。

package com.example.demo;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.example.demo.services.IMessage;
import com.example.demo.services.SayWelcomeService;
@SpringBootApplication
@ComponentScan("com.example.demo.services")
public class AutowireWithMultipleImplementationsApplication {
@Autowired
IMessage sayHelloService;

@Autowired
SayWelcomeService sayWelcome;

@Autowired
IMessage masterService;

public static void main(String[] args) {
SpringApplication.run(AutowireWithMultipleImplementationsApplication.class, args);
}

@PostConstruct
public void init() {
String message;
message=masterService.message("George");
System.out.println("message: \n" + message);

message=sayWelcome.message("george");
System.out.println("message: " + message);
}
}

服务接口(interface)和实现类
接口(interface)IMessage

package com.example.demo.services;
public interface IMessage {
String message(String name);
}

服务SayWelcomeService

package com.example.demo.services;

import org.springframework.stereotype.Service;

@Service
public class SayWelcomeService implements IMessage {

@Override
public String message(String name) {
return "Welcome Dear User - " + name ;
}
}

服务SayHelloService

package com.example.demo.services;

import org.springframework.stereotype.Service;

@Service
public class SayHelloService implements IMessage {

@Override
public String message(String name) {
return "Hello Dear User - " + name ;
}
}

主服务调用其他服务。 Autowiring 按预期工作。
MasterService

package com.example.demo.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MasterService implements IMessage {

@Autowired
List<IMessage> listOfServices;

@Autowired
IMessage sayHelloService;

@Autowired
SayWelcomeService sayWelcome;

@Override
public String message(String name) {

StringBuilder messages = new StringBuilder();
for(IMessage m: listOfServices)
{
messages.append(m.message(name));
messages.append("\n");
}

System.out.println(".....");
System.out.println(sayHelloService.message(name));
System.out.println(sayWelcome.message(name));

return messages.toString();
}
}

现在是测试课。
SayWelcomeServiceTest

package com.example.demo.tests;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.example.demo.services.SayWelcomeService;

public class SayWelcomeServiceTest {

@Autowired
SayWelcomeService sayWelcomeService;

@Test
public void testSayWelcomeMessage()
{
String message = sayWelcomeService.message("George");
assertThat(message, equalTo("Welcome Dear User - George"));
}
}

问题出在上面的类中。 @Autowired 字段(sayWelcomeService)为空。为什么?如何解决?

最佳答案

连接 bean 还需要两个注释。它们是强制性的,否则测试将失败。

这是工作测试类:

    @SpringBootTest
@RunWith(SpringRunner.class)
public class SayWelcomeServiceTest {

@Autowired
private SayWelcomeService sayWelcomeService;

@Test
public void testSayWelcomeMessage()
{
String message = sayWelcomeService.message("George");
assertThat(message, equalTo("Welcome Dear User - George"));
}
}

更多信息请参见 Spring Boot Docs :

Spring Boot provides a @SpringBootTest annotation which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests via SpringApplication.

关于spring - 在 JUnit 测试类中使用 @Autowired 会抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com