0

我正在使用autowiring(@Autowired)在JUnit测试类中注入依赖项,并面临NullPointerException。我想知道在JUnit测试课中/是否可以使用自动装配。否则,如何在测试类中注入bean。我的代码如下 -在JUnit中使用@Autowired测试类会抛出NullPointerException?

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

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);  
    } 
} 

服务接口和实现类
接口即时聊天

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 ; 
    } 
} 

主服务调用其他服务。自动装配按预期工作。
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)为空。为什么?如何解决这个问题?

+0

请看看:https://stackoverflow.com/questions/21878714/how-to-write-junit-test-with-spring-autowire – DevDio

回答

1

需要两个注释来连接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

弹簧引导提供了@SpringBootTest注解,其可以用作 到标准弹簧测试的替代@ContextConfiguration 当您需要Spring Boot功能时进行注释。注释的作用是 通过 SpringApplication创建在测试中使用的ApplicationContext。

相关问题