在Junit测试和spring @configuration中自动装配失败

时间:2019-01-23 12:32:21

标签: java spring junit autowired

我有两个def test_doing_bee_sets_bee_to_22(): foo = Foo() allow(foo).bee do_bee(foo) assert foo.bee == 22 类。我需要从一个配置类到另一个配置类的bean。我已将配置1自动连线到2。一切正常。执行单元测试时,出现以下异常。

@Configuration

要使此功能正常工作,我还需要做些其他事情吗?

下面是测试设置。

setUpContext(com.trafigura.titan.framework.services.messaging.loader.SpringLoadTest)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.MessagingServicesConfig': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.EMSJMSConfig com.xxx.MessagingServicesConfig.emsJmsConfig; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type[com.xxx.EMSJMSConfig] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

最后是测试类,

@Configuration
@Import({MessagingServicesConfig.class,...,EMSJMSConfig.class
})
public class MessagingConfig {}

@Profile("EMS-MESSAGING")
@Configuration
public class EMSJMSConfig {
    @Bean
    public javax.jms.ConnectionFactory jmsSubscriberConnectionFactory() throws JMSException {
        SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(tibjmsConnectionFactory());
        return singleConnectionFactory;
    }
}

@Configuration
public class MessagingServicesConfig {
    @Autowired
    private EMSJMSConfig emsJmsConfig;
    @Bean(destroyMethod = "shutdown")
    public MessagingService messagingService() throws JMSException {
        ...
        ConnectionFactory cf=emsJmsConfig.jmsSubscriberConnectionFactory(); // Getting NPE at this line.
    }
}

1 个答案:

答案 0 :(得分:1)

通过调用Sub SaveAttachments(Mail As MailItem) Dim objOL As Outlook.Application Dim objMsg As Outlook.MailItem 'Object Dim objAttachments As Outlook.Attachments Dim objSelection As Outlook.Selection Dim i As Long Dim lngCount As Long Dim strFile As String Dim strFolderpath As String strFolderpath = "C:\Users\userxx\Documents\Attachments\" On Error Resume Next ' Instantiate an Outlook Application object. Set objOL = CreateObject("Outlook.Application") ' Get the collection of selected objects. Set objSelection = objOL.ActiveExplorer.Selection ' Check each selected item for attachments. If attachments exist, ' save them to the strFolderPath folder and strip them from the item. For Each objMsg In objSelection ' Get the Attachments collection of the item. Set objAttachments = objMsg.Attachments lngCount = objAttachments.Count 'If myItem.SenderEmailAddress = myTargetEmailAddress Then If lngCount > 0 Then ' We need to use a count down loop for removing items ' from a collection. Otherwise, the loop counter gets ' confused and only every other item is removed. For i = lngCount To 1 Step -1 ' Save attachment before deleting from item. ' Get the file name. strFile = objAttachments.Item(i).FileName If strFile Like "*.xls*" Then ' Combine with the path to the Temp folder. strFile = strFolderpath & strFile ' Save the attachment as a file. objAttachments.Item(i).SaveAsFile strFile End If Next i End If 'End If Next ExitSub: Set objAttachments = Nothing Set objMsg = Nothing Set objSelection = Nothing Set objOL = Nothing End Sub自己创建对象,Spring对此一无所知。

此外,您应该具有一个测试配置,该配置将了解您的bean。

使用适当的new加载Runner

SpringContext

@ContextConfiguration(classes = TestConfig.class) @RunWith(SpringRunner.class) class Tests { @Autowired // if needed private MessagingServicesConfig config; } 中,您可以创建TestConfig或从应用程序导入配置:

beans

或者您可以直接引用您的配置类:

@Configuration
@Import({MessagingServicesConfig.class})
public class TestConfig {}

@Configuration
@Import({EMSJMSConfig.class})
public class MessagingServicesConfig {}