Easymock partially taunts (EasyMock ClassExtension), good or bad?

I have written many Mock objects with EasyMock. However, often I find that writing partial layouts takes a lot of time and it doesn't seem to be “correct”.

I would say that his design error is because my class, which I am trying to make fun of, causes several problems in one, so I have to create separate classes to figure out the problems.

What do you think? Partly mocks good or bad? And if good / bad, why? What would you advise if you notice that you cannot scoff at an object because you only want to scoff at several methods?

+3
source share
3 answers

If you find that you are creating partial layouts on a regular basis, this may be a sign that too many states and functionality are being thrown into a small number of classes. This can make your code more complex to maintain and justify, and therefore more difficult to unit test. It can also lead to code duplication or circular dependencies if you later find out that some other component in your system needs a subset of the functions contained in one of your large classes.

-, . , , , . , , Spring Guice, , .

- , . , , , , . . , , , , , , , , , , . , "" . . , , , unit test.

+4

, , ClassA ClassB - ClassA .

, " ". EasyMock ? , , . , ?

, colloborator " ", - . , mocks unit test ( ), - .

0

: , :

• mock , JNI,

public void methodToTest() {
    int result = invokeLibraryCode();
}

// This method will be mocked
int invokeLibraryCode() {
    // This method is native:
    com.3rdparty.Library.invokeMethod(); 
}

• mock , , :

public void methodToTest() {
    Calendar cal = getCurrentDate();
}

// This method will be mocked
Calendar getCurrentDate() {
    return Calendar.getInstance();
}

• mock InputStream, Process :

public void methodToTest(InputStream is) throws IOException {
    int i = is.read(); // is.read() is mocked
}

, , (wrap com.3rdparty.Library , CurrentDateProvider .., , ).

0

Source: https://habr.com/ru/post/1723346/


All Articles