小编Jos*_*tar的帖子

Mockito 模拟无法正常工作

我有以下测试方法:

@RunWith(MockitoJUnitRunner.class)
public class AccountManagerTest {

    @InjectMocks
    private AccountManager accountManager = new AccountManagerImpl(null);

    @Mock
    private AuthStorage authStorage;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    /* REGISTER TESTS */

    @Test
    public void test_whenRegister_withAlreadyExistingEmail_thenDoNotRegister() throws AuthStorageException {
        String email = "foo@bar.com";
        String name = "Foo";
        String password = "123456";
        String password2 = "123456";

        doThrow(new AuthStorageException("Email already in use")).when(authStorage).registerNewUser(Matchers.any());
        assertFalse(accountManager.register(email, name, password, password2));
    }
}
Run Code Online (Sandbox Code Playgroud)

测试以下类方法:

@Override
    public Boolean register(String email, String name, String password, String password2) {
        if (password.equals(password2)) {
            try { …
Run Code Online (Sandbox Code Playgroud)

java testing junit mocking mockito

0
推荐指数
1
解决办法
7797
查看次数

标签 统计

java ×1

junit ×1

mocking ×1

mockito ×1

testing ×1