欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程入门-单元测试代码的编写

程序员文章站 2022-05-25 12:30:57
今天进行单元测试代码的编写 exampleunittest.java import org.junit.test; import static org.junit.assert.*;...

今天进行单元测试代码的编写

exampleunittest.java

import org.junit.test;

import static org.junit.assert.*;

public class exampleunittest {
    @test
    public void addition_iscorrect() throws exception {
        assertequals(4, 2 + 2);
    }
}

soundviewmodeltest.java

import org.junit.before;
import org.junit.test;

import static org.hamcrest.core.is.is;
import static org.junit.assert.*;
import static org.mockito.mockito.mock;
import static org.mockito.mockito.verify;

public class soundviewmodeltest {
    private beatbox mbeatbox;
    private sound msound;
    private soundviewmodel msubject;

    @before
    public void setup() throws exception {
        mbeatbox = mock(beatbox.class);
        msound = new sound("assetpath");
        msubject = new soundviewmodel(mbeatbox);
        msubject.setsound(msound);
    }

    @test
    public void exposessoundnameastitle() {
        assertthat(msubject.gettitle(), is(msound.getname()));
    }

    @test
    public void callsbeatboxplayonbuttonclicked() {
        msubject.onbuttonclicked();
        verify(mbeatbox).play(msound);
    }
}