Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Overview

Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application.

Test Template

Below is a template which you can use to create a new test. All tests should be put inside "src/test/java" source directory of your project.

import static org.mockito.Mockito.mock;
// (...)

import org.junit.Test;
// (...)

public class <ClassBeingTestedName>Test {

    // TESTED CLASS DECLARATION

    // MOCKED CLASS DECLARATIONS

    @Before
    public void init() {
        // THINGS TO DO BEFORE EACH TEST
    }

    @Test
    public void should<TestDescription> throws Exception {
        // given
        // METHOD MOCKS

        // when
        // CALLING TESTED CLASS METHOD WHICH YOU WANT TO TEST

        // then
        // ASSERTIONS
    }

    @After
    public void flush() {
        // THINGS TO DO AFTER EACH TEST (optional)
    }
}

Guidelines

  • In method with "@Before" annotation you should:
    • Initialize tested class. It's always done by using "new ClassName();"
    • Mock all classes used by a tested class. It's done by initializing mocked class variables with "mock(ClassName.class);"
    • Replace classes used by a tested class with the mocked ones. It's done with "setField(testedClassVariable, "variableInsideTestedClass", mockedClassVariable);"
  • During test you need to have full control over mocked class methods used by tested class so you also have to mock them in "given" section
    • It's done with "when(mockedClassVariable.methodBeingMocked(arguments)).thenSomething(argument);"
    • "thenSomething" mentioned above can be for example "thenReturn"
  • Assertions are used to check if method returns value equal to the expected one or if it was called X times
    • Checking values: "assertXXX(expectedValue, actualValue);" where "XXX" can be for example replaced by "Equals"
    • Cheking amount of method calls: "verify(mockedClassVariable, times(amount)).mockedMethod(arguments);"
  • "@Test" annotation has optional parameter "expected" that takes as values subclasses of Throwable
    • It's used to verify if class throws the correct exception and annotation above test method looks like that: "@Test(expected=ExceptionName.class)"

Example

// TODO: TEST EXAMPLE

  • No labels