1

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.

1 Answer 1

1

Create a class called something like AbstractControllerTest and put the shared behavior you want in it. Then your controller test classes can extend from it. You can customize the parameters (like the URL) of the test class through the constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I needed, how didn't I think of this myself.. Anyways, nailed it, Thanks!
Be wary, though. I over-engineered a base class for this purpose recently and it caused more trouble than it was worth. Some things might be worth explicitly repeating instead of trying to get every single shared behavior into one place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.