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

sam*_*ers 2 junit spring dependency-injection inversion-of-control autowired

我正在使用自动装配 (@Autowired) 在 JUnit 测试类中注入依赖项,但面临 NullPointerException。我想知道 JUnit 测试类中是否可以进行自动装配。否则应该如何将bean注入到测试类中。我的代码如下 -

主类/客户端- 自动装配按预期工作。

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);      
    }
}
Run Code Online (Sandbox Code Playgroud)

服务接口和实现类
接口IMessage

package com.example.demo.services;
public interface IMessage {
    String message(String name);
}
Run Code Online (Sandbox Code Playgroud)

服务说欢迎服务

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 ;
    }
}
Run Code Online (Sandbox Code Playgroud)

服务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 ;
    }
}
Run Code Online (Sandbox Code Playgroud)

主服务调用其他服务。自动装配按预期工作。
主服务

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();
    }    
}
Run Code Online (Sandbox Code Playgroud)

现在是测试课。
SayWelcome服务测试

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"));
    }    
}
Run Code Online (Sandbox Code Playgroud)

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

Dev*_*Dio 6

连接 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"));
    }    
}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 文档中的更多信息:

Spring Boot 提供了 @SpringBootTest 注解,当您需要 Spring Boot 功能时,可以将其用作标准 spring-test @ContextConfiguration 注解的替代方案。该注释的工作原理是通过 SpringApplication 创建测试中使用的 ApplicationContext。