1

I was developing the RESTful web service with springmvc4 and spring data jpa.Well, I have about 100+ apis for frontend to pull data.What I am want to do is how to test all of my apis automatically with random data. The apis look like:

@RestController
@Api(tags = "index")
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexService indexService;

    @RequestMapping(value = "/data", method = RequestMethod.GET)
    @ApiOperation(value="today's data",notes="today's data",consumes="application/json",produces="application/json")
    public Object getTodayData() {
        return indexService.getTodayData();
    }

    @RequestMapping(value = "/chartData", method = RequestMethod.GET)
    @ApiOperation(value="charts data",notes="charts data",consumes="application/json",produces="application/json")
    public Object getLast7Data() {
        return indexService.getLast7Data();
    }
}

if I test it with postman one by one,it was waste a lot of time.When we developping,we should make sure the service is ok by ourselves. I have got a solution but which is not satisfied me well. here are my solution:

  • Scaned the controller of the specified package,then use reflection get the annotation of the class,which could get the value of @RequestMapping("/index").

  • Iterate through the method of the class and get the method's annotation the same way,and get the full url.

  • Create random data for request, execute request and log the response. Could anyone provide a solution for this, very appreciate for your help.
Demon Coldmist
  • 2,560
  • 2
  • 13
  • 21
  • If you provide random data, how will you know what output to expect? – Krzysztof Krasoń Jul 18 '16 at 07:40
  • Have you read these posts? http://stackoverflow.com/questions/18392300/tool-for-testing-restful-web-services https://www.jetbrains.com/help/idea/2016.1/testing-restful-web-services.html – mantoviejo Jul 18 '16 at 07:43
  • the "random" data have business limits, for example,"**Name" would give a random name,if "**Price" will give a price.Because we just test the service is ok or not, so don't like test case.just see this http://www.generatedata.com/#t1 – Demon Coldmist Jul 18 '16 at 07:48

3 Answers3

2

I see that you are using swagger in your api, you can use it to generate client code https://github.com/swagger-api/swagger-codegen for automatic testing.

Issam El-atif
  • 2,366
  • 2
  • 17
  • 22
1

Since you are using the Spring framework, you can try the following :

  1. Use Spring Integration Test for testing the API. It spawns an instance of your service and tests against it.
  2. Use RestAssured & JUnit to hit the API and assert the response.
Srikanta
  • 1,145
  • 2
  • 12
  • 22
0

Use RequestMappingHandlerMapping.getHandlerMethods(), which you can simply get with Spring injection, e.g. via @Autowired. This will give you a map RequestMappingInfo->HandlerMethod, which contains all the information you need.

You can run the tests as regular JUnit tests, without the need for postman etc. using Spring integration testing support:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
        @ContextConfiguration(name = "root", locations = "classpath:applicationContext.xml"),
        @ContextConfiguration(name = "web", locations = "classpath:xxx-servlet.xml)
})
public class YourTest extends AbstractTransactionalJUnit4SpringContextTests {...}

In this test, use @Autowired WebApplicationContext and pass it to MockMvcBuilders.webAppContextSetup(webApplicationContext) to create a MockMvc instance. It allows to submit HTTP request to the Spring's MockMvc infrastructure via an easy interface.

Note that Spring's MockMvc framework will not run any real app server such as Tomcat. But this might be exactly what you need, since it is much faster. By default, Spring integration testing framework will only initialize your Spring application context once for all the tests with the same Spring configuration (use @DirtiesContext on a test class or method to signal that a new Spring app context is required after a specific test).

If you feel you need to run an actual app server such as Tomcat within your tests, check maven plugins such as tomcat7-maven-plugin.

Alexander
  • 2,761
  • 1
  • 28
  • 33