我走了:
abstract class IdentifiedEntity<E extends IdentifiedEntity> implements Cloneable {
...
public void updateWith(E that) {
if (this != that) {
if (that.isNew()) {
throw new IllegalArgumentException("Cannot update with a new entity");
}
if (this.isNew()) {
throw new IllegalStateException("Cannot update a new entity");
}
if (this.getId() != that.getId()) {
throw new IllegalArgumentException("IDs do not match");
}
doUpdateWith(that);
}
}
abstract void doUpdateWith(E that);
...
}
public final class User extends IdentifiedEntity<User> {
...
@Override
void doUpdateWith(User that) {
assert that != null;
this.name = that.name;
this.email = that.email;
System.arraycopy(that.password, 0, password, 0, password.length);
this.enabled = that.enabled;
this.caloriesPerDayLimit = that.caloriesPerDayLimit;
}
...
}
问题是我如何对updateWith(...)
进行单元测试,以确保它定义地调用后代中所包含的抽象doUpdateWith(...)
(是的,当然,如果我对检查进行了测试)?
你们这些人!
答案 0 :(得分:1)
创建一个虚拟子类
@Test
public void test() throws Exception {
ConcreteEntity e = Mockito.spy(new ConcreteEntity());
e.updateWith(e);
Mockito.verify(e).doUpdateWith(e);
}
然后像这样测试:
-- Building for: Visual Studio 12 2013
-- The C compiler identification is MSVC 18.0.31101.0
-- The CXX compiler identification is MSVC 18.0.31101.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
CMake Error at C:/Program Files/CMake/share/cmake-3.10/Modules/FindBoost.cmake:1928 (message):
Unable to find the requested Boost libraries.
Boost version: 1.58.0
Boost include path: C:/local/boost_1_58_0
Could not find the following static Boost libraries:
boost_system
boost_filesystem
boost_thread
boost_date_time
boost_chrono
boost_regex
boost_serialization
boost_program_options
Some (but not all) of the required Boost libraries were found. You may
need to install these additional Boost libraries. Alternatively, set
BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
to the location of Boost.
Call Stack (most recent call first):
CMakeLists.txt:113 (find_package)
-- WARNING: Git was not found!
-- Found PythonInterp: C:/Users/chris/AppData/Local/Programs/Python/Python36-32/python.exe (found version "3.6.4")
CMake Warning in CMakeLists.txt:
CMAKE_SKIP_INSTALL_RULES was enabled even though installation rules have
been specified
-- Configuring incomplete, errors occurred!
See also "C:/Users/chris/Documents/GitHub/cryptonote/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/chris/Documents/GitHub/cryptonote/build/CMakeFiles/CMakeError.log".
然而,这样的测试非常特别。它不允许您更改方法的实现。
答案 1 :(得分:0)
在@CoronA的帮助下,我找到了答案。这是:
@Test
public void updateWith() {
User user = this.user.clone().setId(100);
User mock = Mockito.spy(user);
mock.updateWith(user);
Mockito.verify(mock).doUpdateWith(user);
}
非常感谢你们!