我正在尝试学习各种模拟库的细节,PowerMock(特别是EasyMock扩展)是下一个列表.我试图模拟一个构造函数,当我尝试复制它们时,提供的示例没有相同的响应.据我所知,它从不嘲笑构造函数,只是继续进行,就像它是正常的一样.
这是测试类:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Writer.class})
public class FaultInjectionSituationTest {
@Test
public void testActionFail() throws Exception {
FaultInjectionSituation fis = new FaultInjectionSituation();
PowerMock.expectNew(Writer.class, "test")
.andThrow(new IOException("thrown from mock"));
PowerMock.replay(Writer.class);
System.out.println(fis.action());
PowerMock.verify(Writer.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试用EasyMock.isA(String.class)替换"test",但它产生了相同的结果.
这是FaultInjectionSituation:
public class FaultInjectionSituation {
public String action(){
Writer w;
try {
w = new Writer("test");
} catch (IOException e) {
System.out.println("thrown: " + e.getMessage());
return e.getLocalizedMessage();
}
return "returned without throw";
}
}
Run Code Online (Sandbox Code Playgroud)
"作家"只不过是一个类的外壳:
public class Writer {
public Writer(String s) throws IOException {
} …Run Code Online (Sandbox Code Playgroud) 我试图捕获静态类中的'logError'方法(在每个方法/字段的意义上是静态的),并验证它已被同一类中的其他方法调用了若干次.
这个方法是:
public static void logError(Object message){
LOGGER.error(message); // static logger
}
Run Code Online (Sandbox Code Playgroud)
我尝试测试它:
@Test
public void errLogTest() throws Exception{
PowerMockito.mockStatic(X.class);
PowerMockito.doNothing().when(X.class);
X.logError(Mockito.anyString());
X.open();
X.open(); //should log error for opening twice
PowerMockito.verifyStatic(Mockito.times(1));
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,无论我指定了多少次,它都会通过.我删除了模拟行为,并且知道记录器被调用一次,但是我可以PowerMockito.verifyStatic(Mockito.times(9001));改为,它仍然通过.我该如何测试?
对于工作,我偶尔需要监视我创建的服务的输出日志。这些日志是短暂的,并且包含很多我不一定需要的信息。到目前为止,我一直在使用以下方法观察它们:
grep <tag> * | less
Run Code Online (Sandbox Code Playgroud)
其中<tag>是 INFO、DEBUG、WARN 或 ERROR。警告数量大约是错误数量的 10 倍,调试数量大约是警告数量的 10 倍,等等。在大量相关的 DEBUG 消息中很难捕捉到一个 ERROR。例如,我想要一种方法,使所有“警告”消息都出现在终端的左侧,而所有“错误”消息都出现在右侧。
我曾尝试使用 tmux 和 screen,但它似乎不适用于我的开发机器。
我只是看到这是由人们在构建时没有将他们的类对象链接在一起引起的,但我有,而且我不确定是什么问题.
来源:
//test.cpp
#include "Point.h"
#include "Sphere.h"
#include "Scene.h"
int main(){
Sphere s;
Scene sc;
sc.img_plane.upper_left = Point(-1, 1, -3);
sc.img_plane.upper_right = Point(1, 1, -3);
sc.img_plane.lower_left = Point(-1, -1, -3);
sc.img_plane.lower_right = Point(1, -1, -3);
sc.width = 100;
sc.height = 100;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
test.cpp:(.text+0x17): undefined reference to `Sphere::Sphere()'
test.cpp:(.text+0x26): undefined reference to `Scene::Scene()'
test.cpp:(.text+0x192): undefined reference to `Scene::~Scene()'
test.cpp:(.text+0x1a1): undefined reference to `Sphere::~Sphere()'
test.cpp:(.text+0x1bf): undefined reference to `Scene::~Scene()'
test.cpp:(.text+0x1d3): undefined reference to `Sphere::~Sphere()'
Run Code Online (Sandbox Code Playgroud)
Makefile:
CC = g++
SRC …Run Code Online (Sandbox Code Playgroud) 对于集成测试,我需要在java服务客户端中模拟特定方法,而不破坏其中的其余信息.它没有自构造函数,所以这样的解决方案是不可能的:
private DBClient mockClient = new DBClient(alreadyExistingClient){
@Override
void deleteItem(Item i){
//my stuff goes here
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法模拟deleteItem方法,以便在现有的DBClient对象中保留凭据,端点等等?
编辑:mockito在这种情况下不可用
这是我的功能:
friend std::ostream& operator<< (std::ostream& stream, const Path& path) {
std::map<double, glm::vec3>::iterator iter;
for (iter = path.points.begin(); iter != path.points.end(); iter++){
stream << "test" << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>' (or there is no acceptable conversion) c:\users\adam\skydrive\documents\proj\ray\ray\path.h 22 1 ray
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这种问题.说实话,我不知道从哪里开始.我尝试了一些获取迭代器的方法,包括typedef方法,但同样的问题仍然存在.
有什么建议?
这个简单的代码:
public static void Test() throws JsonProcessingException {
Map<Object, Object> request = new HashMap<>();
request.put("id", "test_0001");
request.put("version", 1);
Map<Object, Object> fields = new HashMap<>();
fields.put("uri", "blah/blah");
fields.put("owner", "me");
request.put("fields", request);
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(request));
}
Run Code Online (Sandbox Code Playgroud)
导致此异常:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Enum.ordinal(Enum.java:103)
at com.fasterxml.jackson.databind.MapperFeature.getMask(MapperFeature.java:259)
at com.fasterxml.jackson.databind.cfg.MapperConfig.isEnabled(MapperConfig.java:110)
at com.fasterxml.jackson.databind.SerializationConfig.getAnnotationIntrospector(SerializationConfig.java:404)
at com.fasterxml.jackson.databind.SerializerProvider.getAnnotationIntrospector(SerializerProvider.java:307)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.createContextual(MapSerializer.java:235)
at com.fasterxml.jackson.databind.SerializerProvider._handleContextual(SerializerProvider.java:968)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:447)
at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSerializer(PropertySerializerMap.java:38)
at com.fasterxml.jackson.databind.ser.std.MapSerializer._findAndAddDynamic(MapSerializer.java:516)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:386)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:312)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:26)
etc...
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法弄清楚原因。我通过搜索发现的只是人们因为递归引用而遇到问题,但在这种情况下并非如此。