【问题标题】:How to Mock Service class while writing a Junit for Spring-ws endpoint如何在为 Spring-ws 端点编写 Junit 时模拟服务类
【发布时间】:2019-08-09 14:40:04
【问题描述】:

我创建了一个基于 Spring SOAP 的 Web 服务,它从我的数据库中检索数据,我能够通过 SOAP UI 测试该服务,但现在我正在尝试为该服务添加一些功能,并且我想为该服务添加一些 Junits服务,请在下面找到我的 Endpoint 和 Junit 详细信息。

我的终点类

@Endpoint
public class CountryEndPoint {
    private static final String NAMESPACE_URI = "http://tutorialspoint/schemas";

    @Autowired
    CountryRepository countryRepository;


    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) throws JDOMException {

        Country country = countryRepository.findCountry(request.getName());
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(country);
        return response;
    }
}

Spring-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/web-services
   http://www.springframework.org/schema/web-services/web-services-2.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="com.tutorialspoint" />
    <sws:annotation-driven />

    <bean id="schema"
        class="org.springframework.core.io.ClassPathResource">
        <constructor-arg index="0" value="*.xsd" />
    </bean>
</beans>

Junit-Test 类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-context.xml")
public class CustomerEndPointTest {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ResourceLoader resourceLoader;
    private MockWebServiceClient mockClient;
    private Resource schema = new ClassPathResource("countries.xsd");

    @Before
    public void createClient() {
        mockClient = MockWebServiceClient.createClient(applicationContext);
        GenericApplicationContext ctx = (GenericApplicationContext) applicationContext;
        final XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(ctx);
        definitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
        definitionReader.setNamespaceAware(true);
    }

    @Test
    public void testCountryEndpoint() throws Exception {
        Resource request = resourceLoader.getResource("request.xml");
        Resource response = resourceLoader.getResource("response.xml"); 

mockClient.sendRequest(withPayload(request)).
andExpect(payload(response)).
andExpect(validPayload(schema));
    }
}

我能够毫无问题地运行测试用例,但我的问题是我无法模拟我的服务类 (CountryRepository) 来模拟下面的代码。

Country country = countryRepository.findCountry(request.getName());

有人对此有什么建议吗?

【问题讨论】:

  • 那么,你想模拟 CountryRepository 对象,对吗?

标签: junit mockito spring-test spring-ws


【解决方案1】:

从您的测试用例中,我想您正试图从 web 服务调用内部模拟 CrudRepository 对象:这将被称为集成测试。您需要为单元测试做出选择:

  1. 测试请求,并断言其响应http状态码(例如),
  2. 或者在CountryEndPoint 类中测试getCountry 方法。同时执行这两个选项将被视为集成测试。

我将尝试使用选项 2 考虑单元测试用例来回答。我认为这将为您提供更好的未来见解。

您正在将 CountryRepository 的依赖项注入到您的 CountryEndPoint 类中。你需要在你的测试课上做同样的事情。例如,基本设置是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-context.xml")
public class CustomerEndPointTest {

    @InjectMocks
    private CountryEndPoint countryEndPoint;

    @Mock
    private CountryRepository countryRepository;

    @Test
    public void testCountryEndpoint() throws Exception {
        Mockito.when(countryRepository //...
        countryEndPoint.getCountry(request //build a request object and pass it here.
        //assertions...
    }
}

然后,每当调用来自CountryRepository 的方法时,它都会从模拟中调用。这可能只是因为通过注解注入了模拟。

如果您使用 HTTP 客户端(如您所愿)实际发送请求,则无法模拟在控制器类中调用的方法,因为您没有自己操作和分配实例,因此无法确定什么是模拟,什么不是。

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2013-12-30
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多