简体   繁体   English

“简单” Spring单元测试

[英]“Simple” Spring unit tests

I'm trying to configure unit testing for an inherited Spring -based project. 我正在尝试为继承的基于Spring的项目配置单元测试。 I've tried a few things, but basically I get stuck trying to @Autowired stuff into my test cases. 我已经尝试了一些方法,但是基本上我在尝试将@Autowired东西塞入我的测试用例中时陷入了困境。 Here's my setup: 这是我的设置:

A controller class that looks like this: 如下所示的控制器类:

@Controller("serverService")
@RequestMapping("/rest/server")
@Api(value = "server")
public class ServerServiceImpl extends AbstractServiceImpl implements ServerService {
    @Override
    @RequestMapping(value = "/getTime", method = RequestMethod.GET)
    public @ResponseBody
    GatewayResponse<TimeData> getTime() {...}

ServerService is just an interface that enables interoperation with GWT . ServerService只是一个允许与GWT互操作的接口。 I'm not too worried about unit testing from GWT right now. 我现在不太担心GWT单元测试。

AbstractServiceImpl 's main purpose is to wrap a SOAP based web service that this server is essentially proxying and making mobile friendly. AbstractServiceImpl的主要目的是包装基于SOAP的Web服务,该服务器实际上是代理服务器并使移动设备友好。 The web service is auto-generated by Apache CXF . 该Web服务由Apache CXF自动生成。 AbstractServiceImpl is (roughly) as follows: (大致) AbstractServiceImpl如下:

public class AbstractServiceImpl {
    @Autowired
    private WebServices webServices;

    public WebServices getWebServices() {
        return webServices;
    }

In my test class I have: 在我的测试课中,我有:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:**/applicationContext.xml", "classpath:**/applicationContext-Services.xml"})
@WebAppConfiguration
public class LoginTest {
    @Autowired
    private ServerServiceImpl svc;

    public LoginTest() {
    }

    @Test
    public void validate() {
        assertNotNull(svc);
    }
}

I have no interest in trying to create mock calls to my web service with mock JSON and such. 我对尝试使用模拟JSON等创建对我的Web服务的模拟调用没有兴趣。 I just want to write unit tests that create un-mocked ServerServiceImpl with un-mocked WebServices objects and make calls to a live server. 我只想编写单元测试,以使用未模拟的WebServices对象创建未模拟的ServerServiceImpl并调用实时服务器。

My tests are currently failing because @Autowired is unable to create ServerServiceImpl . 我的测试当前失败,因为@Autowired无法创建ServerServiceImpl I've also tried refactoring my code to use @Autowired for the WebServices and use pass it to the constructor of ServerServiceImpl , but that also fails due to @Autowired . 我还尝试过将代码重构为对WebServices使用@Autowired并将其传递给ServerServiceImpl的构造函数,但是由于@Autowired ,该操作也会失败。

Turns out this was perfectly simple. 事实证明,这非常简单。 Spring will throw errors if you specify a non-existent path for an application context, for instance: 如果为应用程序上下文指定了不存在的路径,Spring将抛出错误,例如:

@ContextConfiguration("classpath:does-not-exist.xml")

The above will create a nice easy error message telling you what the problem is, ie a file not found exception. 上面将创建一个很好的简单错误消息,告诉您问题出在哪里,即找不到文件异常。 On the other hand this code will not: 另一方面,此代码不会:

@ContextConfiguration("classpath:**/does-not-exist.xml")

So my problem was simply that Spring couldn't find the application context XML. 所以我的问题很简单,就是Spring无法找到应用程序上下文XML。 Ultimately I made a copy of the live context, tossed it in src/test/resources and updated my pom.xml 's surefire-plugin and @ContextConfiguration as follows: 最终,我复制了实时上下文,将其扔到src/test/resources并如下更新了pom.xml@ContextConfiguration surefire-plugin@ContextConfiguration

<addtionalClasspathElements>
    <addtionalClasspathElement>${basedir}/src/test/resources</addtionalClasspathElement>
</addtionalClasspathElements>

@ContextConfiguration(locations = { "classpath:applicationContext.xml", "classpath:applicationContext-Services.xml" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM