小编Bap*_*net的帖子

PowerMock,模拟静态方法,然后在所有其他静态上调用实际方法

我正在设置一个类的静态方法.我必须在@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)不幸的是抛出一个RuntimeExceptionif 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)

java junit mockito powermock

35
推荐指数
1
解决办法
2万
查看次数

REST API具有相同的对象,但很轻

我们正在构建一个REST API,我们希望返回相同的对象,但是一个调用是一个"轻量级"版本(没有所有字段)

什么是最佳做法?

第一个案例

第二个案例

第三种情况

第四种情况?

欢迎任何指向REST API文档资源的链接!

谢谢.

api rest

10
推荐指数
2
解决办法
1545
查看次数

第一次调用它时分离处理程序

我想只调用一次事件处理程序,然后将其分离.我试着写:

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)

你有关于我应该如何进行的想法吗?我想过使用布尔标志,但只有当我找不到分离处理程序的方法时才会这样做.

c# events event-handling

3
推荐指数
1
解决办法
146
查看次数

使用WebChannelFactory创建后处置客户端

在我目前的生产代码中,根据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)

我发现这很难看.所以:

  • 我真的需要处理它吗?我的意思是,当您处理工厂时(如果工厂保留对其创建的每个对象的引用可能),它可能会被处理掉?
  • 如果是的话,你有更好的方法吗?

c# wcf dispose webchannelfactory

2
推荐指数
1
解决办法
927
查看次数

如何查询构建的字符串选择

我想做点什么

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)

是唯一的方法吗?或者我可以改进吗?

sql postgresql

2
推荐指数
1
解决办法
48
查看次数