Do I need to test delegate methods? There is a Task class in which there is such a method:
public void setValue(int value, int row, int col) { if (col == variableCount + 1) { limits.set(row - criterionCount, value); return; } if (row < criterionCount) { costs.get(row).set(col, value); return; } if (row >= criterionCount) { weights.get(row - criterionCount).set(col, value); } }In the class Task this method is tested.
There is another class in which there is such a method:
setText(int value, int row, int col){ task.setText(value, row, col); }Do I need to test it?
How to test an anonymous class?
((AbstractDocument) field.getDocument()) .setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int off, String str, AttributeSet attr) throws BadLocationException { fb.insertString(off, str.replaceAll("\\D++", ""), attr); // remove // non-digits } @Override public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr) throws BadLocationException { fb.replace(off, len, str.replaceAll("\\D++", ""), attr); // remove // non-digits } });Do you have to create a non-anonymous class, test it and then create its objects?
3. How to test the state pattern? Interested in the method that tests the getter object state. Here he is:
@Override public TableManager getTableManager() { return tableManager; }The class
tableManagerset depending on the condition. I will show the simplified code.if(isFirstState()){ tableManager = new FirstState(); } else{ tableManager = new SecondState(); }How to test it? It turns out that the tests will have to use
instanceof.
|
1 answer
- Ideally needed. But in practice, testing of such a trivial code (such as getters / setters and such proxy methods) is usually avoided.
- Create a non-anonymous class and test it. Otherwise, the tests are too complicated: you will need to create a lot of objects and correctly configure them in order to test the code that lies somewhere "in the depths". Yes, and not the fact that it will be easy to make a check.
- Yes, check the type of the returned object, this is normal. Sometimes the test of installing
tableManagercan be omitted if this code is trivial, and the top-level class is covered with good tests that willtableManagertype is the wrong type.
|