Last active
August 29, 2015 13:56
-
-
Save mucaho/9123324 to your computer and use it in GitHub Desktop.
TestNG + JMockit - @dataProvider parameters + local @mocked abstract classes / interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* see Boeffi's tutorial: http://boeffi.net/tutorials/local-mock-variables-ein-dritter-weg-in-jmockit/ | |
* | |
* useful for abstract classes / interfaces (shorter than implementing all methods for a "dummy" instance which is dynamically partially mocked afterwards; cleaner than static partial mocking) | |
* take a look at dynamic partial mocking for mocking concrete classes: | |
* http ://jmockit.googlecode.com/svn/trunk/www/tutorial/BehaviorBasedTesting.html#dynamicPartial | |
*/ | |
public final class DatabaseTest { | |
public interface IUser {} | |
public interface IPassword {} | |
public interface IDatabase { | |
public boolean login(IUser user, IPassword pwd); | |
} | |
@DataProvider(name = "loginTestData") | |
public Object[][] loginTestData() { | |
return new Object[][] {{ | |
new IUser(){}, new IPassword(){} | |
},{ | |
new IUser(){}, new IPassword(){} | |
}}; | |
} | |
private IDatabase createDatabaseMock() { | |
return new NonStrictExpectations() { | |
@Injectable IDatabase mockedDatabase; | |
}.mockedDatabase; | |
} | |
@Test(dataProvider = "loginTestData") | |
public void testLogin(IUser user, IPassword password) { | |
final IDatabase mockedDatabase = createDatabaseMock(); | |
new NonStrictExpectations() {{ | |
mockedDatabase.login((IUser) any, (IPassword) any); result = true; | |
}}; | |
assert mockedDatabase.login(user, password) : "Login failed!"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment