我正在使用自动装配(@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);
}
}
服务界面和实施类
界面 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 ;
}
}
主服务呼叫其他服务。自动装配按预期工作 的 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)为null。为什么?如何解决这个问题?
答案 0 :(得分: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中的更多信息:
Spring Boot提供@SpringBootTest注释,可以用作 标准spring-test @ContextConfiguration的替代品 需要Spring Boot功能时的注释。注释的工作原理 通过创建测试中使用的ApplicationContext SpringApplication。