Java 带有 JUnit 测试和依赖注入的 Spring 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33433869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring with JUnit Testing and Dependency Injection does not work
提问by Hakan Kiyar
I try to use Springs own Dependency Injection in a Junit test case:
我尝试在 Junit 测试用例中使用 Springs 自己的依赖注入:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.binarisinformatik.api.AppConfig;
import org.binarisinformatik.satzrechner.SatzRechner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class)
//@SpringApplicationConfiguration(classes = {AppConfig.class})
public class SatzRechnerTest {
@Autowired
private SatzRechner satzRechner; //SUT
@Before
public void setUp() {
// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SatzRechnerTest.class);
//satzRechner=context.getBean(SatzRechner.class);
}
@Test
public void addiere_satz_4komma6_zu_zahlwert_10() {
assertThat("Addition von \"4,6\" ergibt nicht 10!",
satzRechner.summe("4,6"), is(equalTo(10)));
}
Im testing a class names SatzRechner
in which Spring should also autowire some variables. Here is my Class under test:
我正在测试一个类名SatzRechner
,其中 Spring 还应该自动装配一些变量。这是我正在测试的班级:
@Component
public class SatzRechner {
@Autowired //@Inject
private Rechner taschenRechner;
@Autowired
private Zahlenfabrik zahlenfabrik;
public Integer summe(String zeichenSatz) {
return taschenRechner.summe(zahlenfabrik.erzeugeZahlen(zeichenSatz));
}
}
And AppConfig.class
which is using as Configurationfile looks like that:
而AppConfig.class
这是使用作为的ConfigurationFile看起来像这样:
@Configuration
@ComponentScan(value={"org.binarisinformatik"})
public class AppConfig {
}
What is here the problem?
这里有什么问题?
采纳答案by André Blaszczyk
If you want to use a Spring configuration class, this one must have beans definitions. Please find an example below :
如果要使用 Spring 配置类,则此配置类必须有 bean 定义。请在下面找到一个例子:
Test class:
测试类:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.binarisinformatik.api.AppConfig;
import org.binarisinformatik.satzrechner.SatzRechner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class)
public class SatzRechnerTest {
@Autowired
private SatzRechner satzRechner;
@Test
public void addiere_satz_4komma6_zu_zahlwert_10() {
assertThat("Addition von \"4,6\" ergibt nicht 10!",
satzRechner.summe("4,6"), is(equalTo(10)));
}
}
Configuration class :
配置类:
You have to declare @Bean annotated methods. These beans are managed by Spring container.
您必须声明@Bean 注释方法。这些 bean 由 Spring 容器管理。
@Configuration
public class AppConfig {
// Beans present here will be injected into the SatzRechnerTest class.
@Bean
public SatzRechner satzRechner() {
return new SatzRechner();
}
@Bean
public Rechner taschenRechner() {
return new TaschenRechner();
}
@Bean
public Zahlenfabrik zahlenfabrik() {
return new Zahlenfabrik();
}
}
Note : I let you properly handle returned types here and beans parameters (if present in your context).
注意:我让您正确处理此处返回的类型和 beans 参数(如果您的上下文中存在)。
回答by K. Singh
There are two things you have to ensure before you run the test case successfully:
在成功运行测试用例之前,您必须确保两件事:
1) Classes SatzRechner, Rechner & Zahlenfabrik should be under "org.binarisinformatik" package 2) Classes Rechner & Zahlenfabrik should also be annotated with @Component as SatzRechner.
1) 类 SatzRechner、Rechner 和 Zahlenfabrik 应该在“org.binarisinformatik”包下 2) 类 Rechner 和 Zahlenfabrik 也应该用 @Component 注释为 SatzRechner。