Java @自动连线Bean在Spring引导单元测试中为空

Java @自动连线Bean在Spring引导单元测试中为空,java,unit-testing,spring-boot,Java,Unit Testing,Spring Boot,我不熟悉JUnit和自动化测试,我真的很想开始自动化测试。这是一个Spring引导应用程序。我使用了基于Java的注释样式,而不是基于XML的配置 我有一个测试类,我想在其中测试一个基于用户输入检索响应的方法 测试等级: @RunWith(SpringRunner.class) @SpringBootTest public class SampleTest(){ @Autowired private SampleClass sampleClass; @Test public

我不熟悉JUnit和自动化测试,我真的很想开始自动化测试。这是一个Spring引导应用程序。我使用了基于Java的注释样式,而不是基于XML的配置

我有一个测试类,我想在其中测试一个基于用户输入检索响应的方法

测试等级:

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

  @Autowired
  private SampleClass sampleClass;

  @Test
  public void testInput(){

  String sampleInput = "hi";

  String actualResponse = sampleClass.retrieveResponse(sampleInput);

  assertEquals("You typed hi", actualResponse);

  }
}
在我的“SampleClass”中,我自动连接了这样一个bean

@Autowired
private OtherSampleClass sampleBean;
在我的“OtherSampleClass”中,我注释了如下方法:

@Bean(name = "sampleBean")
public void someMethod(){
....
}
我遇到的问题是,当我尝试在没有
@RunWith
@springbootest
注释的情况下运行测试时,当我尝试运行测试时,注释为
@Autowired
的变量为空。当我尝试使用那些注释RunWith&SpringBootTest运行测试时,我得到一个

BeanCreationException导致的IllegalStateException:创建时出错 名为“sampleBean”且未能加载应用程序上下文的bean 由BeanInstantiationException引起

当我试着像用户一样使用代码时,代码会“正常”工作,所以我总是可以用这种方式进行测试,但我认为自动化测试对程序的寿命有好处


在这方面,我使用了帮助。

最好尽可能将Spring排除在您的测试之外。不要自动连接bean,只需将它们创建为常规对象即可

OtherSampleClass otherSampleClass = mock(OtherSampleClass.class);
SampleClass sampleClass = new SampleClass(otherSampleClass);
但为此,您需要使用构造函数注入,而不是字段注入,这将提高可测试性

替换这个

@Autowired
private OtherSampleClass sampleBean;
用这个

private OtherSampleClass sampleBean;

@Autowired
public SampleClass(OtherSampleClass sampleBean){
    this.sampleBean = sampleBean;
}
查看其他代码示例的答案

无需插入(
@Autowired private SampleClass-SampleClass;
)您正在测试的实际类,并删除SpringBootTest注释,
用于集成测试用例的SpringBootTest注释。
查找以下代码将对您有所帮助

@RunWith(SpringRunner.class)
public class SampleTest(){

  private SampleClass sampleClass;

下面的配置适合我

文件:
build.gradle

testCompile("junit:junit:4.12")
testCompile("org.springframework.boot:spring-boot-starter-test")
文件:
MYServiceTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = Application.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class MYServiceTest {

    @Autowired
    private MYService myService;

    @Test
    public void test_method() {
        myService.run();
    }
}

请添加您的
SampleClass
。用
@Bean
注释的方法应该返回一个对象,即新的Spring Bean。Spring注入您的自动连接Bean。如果您没有使用spring runner运行,那么就没有任何东西可以注入,因此成员将保持其默认值(null)。使用spring运行时,请显示更多异常。很可能是找不到类路径或属性失败作为根本原因。使用模拟对象进行测试并不理想。有了spring boot test和现代测试框架,spring web客户端就可以放心了。不需要模拟任何东西,最重要的问题是为什么自动布线不起作用,而不是如何测试类。@Yogi在测试控制器、进行集成测试和E2E测试时,这是正确的,但是对于单元测试,您应该尽可能避免使用spring,因为它会产生大量的开销。这是如何回答有关如何执行
@Autowired
的问题的呢?