dimanche 23 avril 2017

Use static initialization block with Mockito

I am starting up with Mockito as my mocking framemork. I try to mock some custom class with it:

//usage
@Mock
private LoginAttempt loginAttempt;

and the LoginAttempt class:

public class LoginAttempt {
    private static LoginAttempt loginAttempt;

    static {
        loginAttempt = new LoginAttempt();
        loginAttempt.setOs(TEST_GLOBALS.OS);
        loginAttempt.setBrowser(TEST_GLOBALS.BROWSER);
        loginAttempt.setDevice(TEST_GLOBALS.DEVICE);
        loginAttempt.setOsVersion(TEST_GLOBALS.OS_VERSION);
        loginAttempt.setBrowserVersion(TEST_GLOBALS.BROWSER_VERSION);
    }
...

But when I debug my test cases, the loginAttempt var is empty. What am I doing wrong?

I saw in tutorials, that I should do something like this:

private static LoginAttempt loginAttempt = new LoginAttempt();

But what if I want to preinitialize some field values?

EDIT my loginAttempt is not null, but the values I assigned in the static block are not initialized.

Aucun commentaire:

Enregistrer un commentaire