1

I am new to TestNG. I am writing classes containing a unit test case for spring boot application, and running them using eclipse TestNG plugin. For every different api or method I need to create different class containing test. Can we avoid creating different classes everytime? If yes please guide how can I write.

Below is code snippet for one of the api,

@SpringBootTest
@DirtiesContext
public class TestAdd {

    private Integer id; 

    @BeforeTest
    public void beforeAddReturnsObjectTest() {

    }

    @Test
    public void testAddReturnsObject() throws Exception {
        Test1 test1 = new Test1();
        test1.setName("Mohit");
        ResponseEntity<Test1> entity = new TestRestTemplate()
                .postForEntity("http://localhost:8080/test/", test1, Test1.class);
        id = entity.getBody().getId();
        assertThat(entity.getBody().getName()).isEqualTo(test1.getName());
    }

    @AfterTest
    public void AfterAddReturnsObjectTest() {
        TestRestTemplate entity = new TestRestTemplate();
        entity.delete("http://localhost:8080/test/" + id, Test1.class);
    }
}

Any help would be appreciated!!

  • Store the test for different classes is a good way to structure tests. I recommend using the **MoreUnit** eclipse plugin. With it you can switch between method and its test with **strg-J**. If the test-class does not yet exists it asks you if it should generate the class for you. – MrSmith42 Jul 13 '16 at 14:04
  • If you mean one method per class that's not required. You can write as many @Test annotated methods in one class as you want. But writing all your tests in one class is not a good idea – Lenar Dec 29 '17 at 16:34

0 Answers0