vendredi 27 juillet 2018

How to perform Spring Rest controller testing using same test class with different data?

I am currently trying to write very simple integration tests for my Spring REST controllers.

Lets say my test class looks something like this:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RealNewTest2 {

    @Autowired
    private MockMvc mvc;

    @Test
    public void test() throws Exception {
        mvc.perform(
                get(GET_URL).
                with(httpBasic("user","pass"))).
        andExpect(status().isOk());


        System.out.println("Test done.");
    }

}

I want to perform very basic test case that would test all the calls(GET,POST,PUT,DELETE) etc. All of my REST controllers are very alike. The goal that I am thinking is that I would have test data for all controllers like the JSON object that it uses when doing PUT test and then it would have the URL/Mapping that the controller uses. All of my controllers Mappings are the same expect the last part for example mysite/accounts and mysite/countries.

So is there any way that i could write one test case that would perform all of these REST calls and then just run it again with different url and JSON object so i wouldn't have to write so many test cases since they are only VERY basic tests and are basically exactly the same expect for the JSON object and REST URL.

Aucun commentaire:

Enregistrer un commentaire