Search code examples
javatestngjmockit

Use @DataProvider with Jmockit


I'm using Testng 6.8.5 and Jmockit 1.4 for my project. I want to use testng @DataProvider with Jmockit @Mocked annotation with test parameter level.

@Test
public void testRemove(@Mocked Creator) throws Exception {
   //Test Code
}

I want to use above test with different data sets (using @DataProvider), but when I change the method signature to:

public void testRemove(@Mocked Creator creator, int id, String name)

where id and name are provied by the DataProvider, TestNG fails saying that the DataProvider only provides two parameters, not three.

Does anyone know how to achieve this?


Solution

  • Your mocked object must be a fied of the test class.

    public class UserTest {
    
        @Mocked
        Creator creator;
    
    
        @DataProvider(name = "my data provider")
        public Oject[][] dataProvider() {
        //code data proviver
        }
    
    
        @Test(dataProvider = "my data provider")
        public void testRemove(int id, String name) {
            new Expectations() {{
                creator.doSomething(...);
                result = expectedResult;
    
                //other expectation
            }};
            //other test code
        }
    }