小编Tus*_*nde的帖子

使用mockito库模拟java中的final类

我有一个最终类,它有一个我想执行特定操作的方法。因此我想创建最终类的对象。但我无法创建它,以下是我的课程。

public final class A {

     private String name;

     A(String name){
       this.name = name;
     }

     public String getName(){
       return name;
     }
}
Run Code Online (Sandbox Code Playgroud)

在我的 junit 测试用例中,我想创建该类的对象,如下所示

 Class TestA{

      @Test
      public void testA(){
          A a = mock(A.class);

          when(a.getName()).then("ABC"); //on this line i am getting exception
      }
 }
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用 new 关键字,但不起作用。那么有没有办法创建最终类的模拟对象呢?

在我面临异常之后,

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class A
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
    at com.rocket.map.resources.TestA.testA(TestA.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) …
Run Code Online (Sandbox Code Playgroud)

java junit mockito

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

在Elasticsearch中将多个自定义分析器设置为单个字段

我在ElasticSearch环境中工作,我已经在本地计算机上为版本5.4.3安装了elasticsearch。我正在尝试通过定义一些设置以及映射来创建索引。以下是我的设置和映射,

{  
   "settings":{  
      "index":{  
         "analysis":{  
            "analyzer":{  
               "index_analyzer":{  
                  "filter":[  
                     "standard",
                     "lowercase",
                     "asciifolding"
                  ],
                  "tokenizer":"standard"
               },
               "autocomplete":{  
                  "type":"custom",
                  "tokenizer":"standard",
                  "filter":[  
                     "lowercase",
                     "autocomplete_filter"
                  ]
               },
               "search_analyzer":{  
                  "filter":[  
                     "standard",
                     "lowercase",
                     "asciifolding"
                  ],
                  "tokenizer":"standard"
               },
               "sortable":{  
                  "filter":"lowercaseFilter",
                  "tokenizer":"keyword",
                  "type":"custom"
               }
            },
            "filter":{  
               "lowercaseFilter":{  
                  "type":"lowercase"
               },
               "autocomplete_filter":{  
                  "type":"edge_ngram",
                  "min_gram":1,
                  "max_gram":20
               }
            },
            "tokenizer":{  
               "keyword":{  
                  "type":"keyword"
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的映射

{  
   "geo_data":{  
      "_all":{  
         "enabled":true,
         "index_analyzer":"index_analyzer",
         "search_analyzer":"search_analyzer"
      },
      "properties":{  
         "subscriber_level":{  
            "analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
            "type":"text"
         },
         "att_id":{  
            "analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
            "type":"text"
         },
         "id":{  
            "include_in_all":false,
            "type":"text"
         },
         "name":{  
            "analyzer":"index_analyzer,search_analyzer,autocomplete_analyzer",
            "type":"text"
         }, …
Run Code Online (Sandbox Code Playgroud)

curl elasticsearch elasticsearch-mapping elasticsearch-analyzers

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