/
Unit tests Unit tests

Unit tests - PowerPoint Presentation

test
test . @test
Follow
401 views
Uploaded On 2017-08-12

Unit tests - PPT Presentation

Landon Cox April 25 2017 Things covered this semester Design patterns code structure Decorator Openclose principle APIs external libraries services Firebase SQLite Volley Project ID: 578251

org import string test import org test string static public junit class mockito fake mmockcontext android unit context runwith

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Unit tests" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Unit tests

Landon Cox

April

25

,

2017Slide2

Things covered this semester

Design patterns (code structure)

Decorator

Open/close principle

APIs (external libraries, services)

Firebase

SQLite

Volley

Project

management

tools

Android studio

git

What’s missing?Slide3

Topics this semester

Design patterns (code structure)

Decorator

Open/close principle

APIs (external libraries, services)

Firebase

SQLite

Volley

Project management tools

Android studio

git

What’s missing? Testing!Slide4

Unit testing

Unit tests are small

Exercise a single method or class

Test behavior in isolation from rest of program

Run tests at build time

Should be integrated into

gradle

Sanity check any changes you make

Fail a unit test? Fail the build.Slide5

Unit testing in Android

Local unit tests

Run locally on your machine

Cannot depend on a launched app

Instrumented unit tests

Run on an emulator or device

Have access to Context object

https://

developer.android.com/training/testing/unit-testing/local-unit-tests.html

http://

www.vogella.com

/tutorials/

AndroidTesting

/

article.htmlSlide6

Unit testing in Android

Local unit tests

Run locally on your machine

Cannot depend on a launched app

Instrumented unit tests

Run on an emulator or device

Have access to Context object

https://

developer.android.com/training/testing/unit-testing/local-unit-tests.html

http://

www.vogella.com

/tutorials/

AndroidTesting

/

article.htmlSlide7

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;

import

static

org.junit.Assert.

assertTrue

;

public

class

EmailValidatorTest

{

   

@Test

   

public

void

emailValidator_CorrectEmailSimple_ReturnsTrue

() {

       

assertThat

(

EmailValidator

.isValidEmail

(

"

name@email.com

"

), is(

true

));

   

}

}Slide8

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

Import stuff from

org.junit

.*Slide9

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

Annotate method w/ @TestSlide10

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

Use assertions in test methodSlide11

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

Use assertions in test methodSlide12

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

These tests are useful for simple, stateless methods, like code that checks valid emails. Slide13

Example local test

import

org.junit.

Test

;

import

java.util.regex.

Pattern

;

import

static

org.junit.Assert.

assertFalse

;import static org.junit.Assert.assertTrue;public class EmailValidatorTest {    @Test    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {        assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));    }}

Tests are also pretty limited

…Slide14

Unit testing in Android

Local unit tests

Run locally on your machine

Cannot depend on a launched app

Instrumented unit tests

Run on an emulator or device

Have access to Context object

https://

developer.android.com/training/testing/unit-testing/local-unit-tests.html

http://

www.vogella.com

/tutorials/

AndroidTesting

/

article.htmlSlide15

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Import stuff from

org.junit

.*Slide16

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Import stuff from

org.mockito

.*Slide17

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Import stuff from

org.hamcrest

.*Slide18

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Annotate class with @

RunWithSlide19

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Annotate mock objects with @MockSlide20

Example instrumented test

import static

org.hamcrest.MatcherAssert.

assertThat

;

import static

org.hamcrest.CoreMatchers

.*;

import static

org.mockito.Mockito

.*;

import

org.junit.

Test

;

import

org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;import android.content.SharedPreferences;@RunWith(MockitoJUnitRunner.class)public class UnitTestSample {    private static final String FAKE_STRING = "HELLO WORLD";   

@Mock

   

Context

mMockContext

;

   

@Test

   

public void

readStringFromContext_LocalizedString

()

{

    }

}

Annotate test methods with @TestSlide21

Example instrumented test

import

android.content.

SharedPreferences

;

@

RunWith

(

MockitoJUnitRunner

.class

)

public class

UnitTestSample

{

    private static final String FAKE_STRING = "HELLO WORLD";    @Mock    Context mMockContext;    @Test    public void readStringFromContext_LocalizedString() {        // Given a mocked Context injected into the object under test...        when(mMockContext.getString(R.string.hello_word))                .thenReturn

(FAKE_STRING);

       

ClassUnderTest

myObjectUnderTest

=

new

ClassUnderTest

(

mMockContext

);

       

// ...when the string is returned from the object under test...

       

String

result =

myObjectUnderTest.getHelloWorldString

();

       

// ...then the result should be the expected one.

       

assertThat

(result, is(FAKE_STRING));

    }

}

Define behavior for the

mock objectSlide22

Example instrumented test

import

android.content.

SharedPreferences

;

@

RunWith

(

MockitoJUnitRunner

.class

)

public class

UnitTestSample

{

    private static final String FAKE_STRING = "HELLO WORLD";    @Mock    Context mMockContext;    @Test    public void readStringFromContext_LocalizedString() {        // Given a mocked Context injected into the object under test...        when(mMockContext.getString(R.string.hello_word))                .thenReturn

(FAKE_STRING);

       

ClassUnderTest

myObjectUnderTest

=

new

ClassUnderTest

(

mMockContext

);

       

// ...when the string is returned from the object under test...

       

String

result =

myObjectUnderTest.getHelloWorldString

();

       

// ...then the result should be the expected one.

       

assertThat

(result, is(FAKE_STRING));

    }

}

When accessing

R.strin

g.hello_world

,

r

eturn “HELLO_WORLD”Slide23

Example instrumented test

import

android.content.

SharedPreferences

;

@

RunWith

(

MockitoJUnitRunner

.class

)

public class

UnitTestSample

{

    private static final String FAKE_STRING = "HELLO WORLD";    @Mock    Context mMockContext;    @Test    public void readStringFromContext_LocalizedString() {        // Given a mocked Context injected into the object under test...        when(mMockContext.getString(R.string.hello_word))                .thenReturn

(FAKE_STRING);

       

ClassUnderTest

myObjectUnderTest

=

new

ClassUnderTest

(

mMockContext

);

       

// ...when the string is returned from the object under test...

       

String

result =

myObjectUnderTest.getHelloWorldString

();

       

// ...then the result should be the expected one.

       

assertThat

(result, is(FAKE_STRING));

    }

}

Now create an instance of the class you want to test.Slide24

Example instrumented test

import

android.content.

SharedPreferences

;

@

RunWith

(

MockitoJUnitRunner

.class

)

public class

UnitTestSample

{

    private static final String FAKE_STRING = "HELLO WORLD";    @Mock    Context mMockContext;    @Test    public void readStringFromContext_LocalizedString() {        // Given a mocked Context injected into the object under test...        when(mMockContext.getString(R.string.hello_word))                .thenReturn

(FAKE_STRING);

       

ClassUnderTest

myObjectUnderTest

=

new

ClassUnderTest

(

mMockContext

);

       

// ...when the string is returned from the object under test...

       

String

result =

myObjectUnderTest.getHelloWorldString

();

       

// ...then the result should be the expected one.

       

assertThat

(result, is(FAKE_STRING));

    }

}

Call a method on that instance.Slide25

Example instrumented test

import

android.content.

SharedPreferences

;

@

RunWith

(

MockitoJUnitRunner

.class

)

public class

UnitTestSample

{

    private static final String FAKE_STRING = "HELLO WORLD";    @Mock    Context mMockContext;    @Test    public void readStringFromContext_LocalizedString() {        // Given a mocked Context injected into the object under test...        when(mMockContext.getString(R.string.hello_word))                .thenReturn

(FAKE_STRING);

       

ClassUnderTest

myObjectUnderTest

=

new

ClassUnderTest

(

mMockContext

);

       

// ...when the string is returned from the object under test...

       

String

result =

myObjectUnderTest.getHelloWorldString

();

       

// ...then the result should be the expected one.

       

assertThat

(result, is(FAKE_STRING));

    }

}

Assert that it behaved as expected