[英]Nested autowired field stays null in JUNIT test
I have a factory pattern implemented and wanted to test this. 我有一个工厂模式实现,并想测试这个。 Except the fields in this factory method are not getting autowired. 除了此工厂方法中的字段未进行自动装配。 It seems like the @Autowired attribute in the factory class is staying null. 看起来工厂类中的@Autowired属性保持为null。 I can't use @SpringBootTest annotation because of the blockchain configuration file that gets loaded then 我不能使用@SpringBootTest注释,因为然后加载了区块链配置文件
Below is the code of the service factory, the parserfactory gets autowired correctly in the test. 下面是服务工厂的代码,parserfactory在测试中正确自动装配。 The problems is with the autowired fields of the parserfactory 问题在于parserfactory的自动连接字段
@Service
@Slf4j
public class ParserFactory {
@Autowired
OsirisParser osirisParser;
public Parser getParser(String system) {
if (system == null) {
return null;
}
if (system.equalsIgnoreCase("Progress")) {
return ProgressCreateService();
}
if (system.equalsIgnoreCase("Osiris")) {
log.debug("Osiris parsen creëren");
return OsirisCreateService();
}
return null;
}
public OsirisParser OsirisCreateService() {
return osirisParser;
}
public OsirisParser ProgressCreateService() {
return new OsirisParser("ProgressParser");
}
The test 考试
@RunWith(SpringRunner.class)
public class FactoryTest {
@Mock
ParserFactory serviceCallFactory;
@Test
public void testCreateOsirisServiceSuccesFull() {
Parser serv = serviceCallFactory.getParser("Osiris");
assertThat(serv, instanceOf(OsirisParser.class));
}
@Test
public void testCreateProgressServiceSuccesFull() {
Parser serv = serviceCallFactory.getParser("Progress");
assertThat(serv, instanceOf(ProgressParser.class));
}
@Test
public void testCreateProgressServiceUnSuccessFull() {
Parser serv = serviceCallFactory.getParser("Progrddess");
assertThat(serv, is(not(instanceOf(OsirisParser.class))));
}
@Test
public void testCreateWhenStringIsNotCorrect() {
Parser serv = serviceCallFactory.getParser("0$iri$");
assertThat(serv, is(nullValue()));
}
@Test
public void testCreateWhenStringIsNull() {
Parser serv = serviceCallFactory.getParser("");
assertThat(serv, is(nullValue()));
}
}
You do not have any spring context in your test class, which means that you are testing a POJO with no spring initialization to it, so the autowiring does not happen and the field is null. 您的测试类中没有任何弹簧上下文,这意味着您正在测试没有弹簧初始化的POJO,因此不会发生自动装配,并且该字段为空。
There are several ways to solve it: 有几种方法可以解决它:
If you want to test it like a POJO, set the field on the tested class. 如果要像POJO一样测试它,请在测试类上设置字段。 you can use @InjectMocks
, ReflectionTestUtils.setField
in your current implementation, or move the @Autowire
to a constructor or setter and create the class in the test with the field. 您可以在当前实现中使用@InjectMocks
, ReflectionTestUtils.setField
,或者将@Autowire
移动到构造函数或setter,并使用该字段在测试中创建类。 I vote for autowiring on a constructor 我投票支持构造函数的自动装配
If you want it to be tested as a spring component, define a @TestConfiguration
and in there either import your real configuration and override the beans you don't want to be used, or use a completely different spring configuration for the test. 如果您希望将其作为弹簧组件进行测试,请定义@TestConfiguration
然后导入您的实际配置并覆盖您不想使用的bean,或者使用完全不同的弹簧配置进行测试。
either way works, choose the one that suits you more 无论哪种方式都有效,请选择更适合您的方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.