我正在设置一个类的静态方法.我必须在@Before
注释的JUnit设置方法中执行此操作.
我的目标是设置类来调用实际方法,除了我明确模拟的那些方法.
基本上:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class);
// mock out certain methods...
when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5);
// Now have all OTHER methods call the real implementation??? How do I do this?
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在StaticUtilClass
方法中,public static int someStaticMethod(String s)
不幸的是抛出一个RuntimeException
if null
值.
所以我不能简单地将调用真实方法的明显路线作为默认答案,如下所示:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods
// The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
// Even …
Run Code Online (Sandbox Code Playgroud) 我们正在构建一个REST API,我们希望返回相同的对象,但是一个调用是一个"轻量级"版本(没有所有字段)
什么是最佳做法?
第一个案例
第二个案例
第三种情况
第四种情况?
欢迎任何指向REST API文档资源的链接!
谢谢.
我想只调用一次事件处理程序,然后将其分离.我试着写:
EventHandler handler = (s, e) =>
{
// Do something
//
// blabla
// Detach the handler
SizeChanged -= handler;
};
SizeChanged += handler;
Run Code Online (Sandbox Code Playgroud)
但是在线上SizeChanged -= handler
我得到了这个错误
Use of unassigned local variable 'handler'
Run Code Online (Sandbox Code Playgroud)
你有关于我应该如何进行的想法吗?我想过使用布尔标志,但只有当我找不到分离处理程序的方法时才会这样做.
在我目前的生产代码中,根据msdn上的文档,创建客户端的方法就是这样
using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
{
IServiceInterface client = cf.CreateChannel();
client.CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)
鉴于我有这个界面:
public interface IServiceInterface
{
void CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)
但是我注意到WebChannelFactory创建的对象客户端也实现了IDisposable.所以我也要处理这个对象.我没有找到任何其他方式:
using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
using(IDisposable client = (IDisposable)cf.CreateChannel())
{
((IServiceInterface)client).CallTheMethod();
}
Run Code Online (Sandbox Code Playgroud)
我发现这很难看.所以:
我想做点什么
SELECT * FROM (
SELECT ('reword#' || reword) || reword_faq as reword FROM me_review_entries re
) as re
WHERE re.reword = 'reword#2#SOME_FAQ'
Run Code Online (Sandbox Code Playgroud)
我试着这样做
SELECT ('reword#' || reword) || reword_faq as foo FROM me_review_entries re
WHERE foo = 'reword#2#SOME_FAQ'
Run Code Online (Sandbox Code Playgroud)
但我得到:
ERROR: column "foo" does not exist
LINE 2: WHERE foo = 'reword#2#SOME_FAQ'
Run Code Online (Sandbox Code Playgroud)
是唯一的方法吗?或者我可以改进吗?