配置的 JUnit 测试用例

JUnit Test Cases for Config

我有一个 javaconfig 文件,如下所示:

@Configuration
public class ServiceConfig {
    @Autowired
    FooBean someBean;

    @Bean
    @Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public FooService fooService() {
        return new FooServiceImpl(someBean, fooB());
    }

    private Foo fooB() {
        return new FooB();
    }
}

并且我已经基于 this stack answer 创建了一个像这样的 junit 测试文件:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServiceConfig.class)
public class ServiceConfigTest {

}

但我有几个问题:

  1. 我应该用这个 junit 测试文件来测试我所有的配置文件吗?我总共有 4 个配置文件,包括 ServiceConfig 文件,所以在 ContextConfiguration 中我应该把它们全部列出来还是对每个单独进行 junit 测试?

  2. 我应该在这里测试什么?我试过阅读这篇 spring guide 但我不太明白我应该在这里测试什么行为......只是如果某些东西成功自动装配?

测试您的系统或单个功能单元的预期行为。作为其中的一部分,将测试配置。

您不需要进行测试来确保某些东西接线正确。这应该是基于您正在测试的功能有效的暗示。

您可以使用 @Import.

将多个配置聚合为一个配置 class

Should I test all my config files with this one junit test file? I have 4 config files in total including the ServiceConfig file, so in the ContextConfiguration should I just list them all out or have a junit test for each one indivually?

在您的测试 class 中,@ContextConfiguration 必须位于 class 的根上。 因此,为了测试每个配置,您必须通过 Configuration.

创建一个测试 class

What am I supposed to test here? I tried reading this spring guide but I'm not really understand what behavior I should test here....just if something gets autowired successfully?

测试自动装配是否成功似乎不是很有用。 Spring 功能可以作为单元测试。如果你想对这些 classes 进行单元测试,你应该测试你自己的处理。目前,你还没有真正。所以,我不确定测试它们是否有很大的价值。