简体   繁体   English

Spring 引导中 Rest 客户端 Junit 的 InvalidUseOfMatchersException

[英]InvalidUseOfMatchersException for Rest Client Junit in Spring Boot

I am trying to write Junit test case using mockito for Rest Client in Spring Boot.我正在尝试使用 mockito 为 Rest 客户端在 Z38008DD81C2F4D798ZECAFEDE16 Getting the error while mocking the response as shown below:在 mocking 响应时出现错误,如下所示:

Mocking Response using Mockito: Mocking 使用 Mockito 的响应:

   UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(Url);

        Mockito.when(this.restTlsTemplate
                .exchange(Mockito.eq(builder.toString()), HttpMethod.GET, Mockito.any(), RestResponse.class)
                .getBody()).thenReturn(response());

Error getting for above mocking response:获取上述 mocking 响应时出错:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 2 recorded:
-> at (ApplicationTests.java:54)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

Can anyone please check this and help me on this.任何人都可以检查这个并帮助我。

If you use a matcher such as Mockito.eq or Mockito.any for one of the parameters in your stubbed or verified method call, you must use a matcher for ALL the parameters.如果您对存根或验证方法调用中的参数之一使用匹配器,例如Mockito.eqMockito.any ,则必须对所有参数使用匹配器。 This is because of the way Mockito arranges its matchers in an internal stack.这是因为 Mockito 将其匹配器排列在内部堆栈中的方式。 So you should write所以你应该写

Mockito.when(this.restTlsTemplate
            .exchange(Mockito.eq(builder.toString()), Mockito.eq(HttpMethod.GET), Mockito.any(), Mockito.eq(RestResponse.class))
            .getBody()).thenReturn(response());

where Mockito.eq has been placed around each parameter that you want to match exactly.其中Mockito.eq已放置在您要精确匹配的每个参数周围。

Neden kullanıyoruz?内登 kullanıyoruz?

  1. Metodlarımızı interfacelerimizin içerisine metodlarımızın imzalarını tanımlıyoruz daha sonra bu interfacelerimizi inplamente ederek kullanıyoruz Metodlarımızı interfacelerimizin içerisine metodlarımızın imzalarını tanımlıyoruz daha sonra bu interfacelerimizi inplamente ederek kullanıyoruz
interface Veritabanı{
 //interfacelerin içinde metodların body dediğimiz yer kıvırcıkların içi , imzası
//dediğimiz yerde ekledir metodların imzası olur bodysi olmaz biz burada metodun imzasını belirledik
public void ekle();
public void sil();
public void guncelle();
public void getir();

VeritabaniImpl clasını  oluşturduk şimdi bi class bir interface i implemente ediyor
//
Bir interface sınıfında sadece metotlar açıklanır. Bu metotların gövdeleri boştur. Alt sınıflar bu metotların gövdeleri için gerekli kodu oluştururlar (implement ederler). Metot gövdesi olmayan bir interface sınıftan nesne oluşturulamaz, çünkü sadece metot bildirimine sahip bir nesnenin hiçbir işlevsel görevi olamaz.
//interfaceleri en çok metod tanımı yapmak için kullanılırlar 
kim bir interface i implemente ederse interfacesin içindeki clası override etmek zorundadır
bir clas birden fazla interface i implemente edebilir 
bir class sadece bir classı miras alabilir
 bir class yüzlerce interface implemente edebilir
public class VeritabnıImpl implements Veritabani,Interface2{

addunplmt e bas 4 metodu getir interfaceden 
}
public interface Interface2{
public void test1();
  1. İnterface isimlendirmesinde başına I harfi konulur界面 isimlendirmesinde başına I harfi konulur
Interface örnek kullanım alanı ile eş değer
main classdayım 

interface1= IMuhendis
public void bilgileriYazdir();
public void askerlikYapildiMi(boolean deger)
public class BilgisayarMuhendisi implements  IMuhendis{
private string isim;
private string soyisim;
private double maas;
private string[] diller;

@override işlemi
bilgileri yazdır da :  syso("isim :"+getisim());
syso("soyisim : "+getsoyisim());
syso("tecrübe :" +getTecrübe();
***maaş**
syso("Diller  :");
for(string dil : diller )
syso(dil+ "  , " );
getter and setter  privateler için
constroctor oluştur 
public class MainClass { 
    public static void main(String[] args) { 
    SoftwareDeveloper softwareDeveloper = new SoftwareDeveloper("Ayşenur", "Aydoğdu", 20000,new String[] {"türkçe","ingilizce","İspanyolca"}); 
            //ctrl space yapınca geri geliyor  
    JuniorSoftwareDeveloper juniorSoftwareDeveloper = new JuniorSoftwareDeveloper("Kübra ", "Özdemir", 100,new String[] {"Türkçe","İngilizce","Azerice"}); 
    } 
}

önemli kural interface i implemente eden classlardan nesne ürettiği zaman aynı tipte yakalamak yerşne implemente ettiği interface olarak tanımlayanilirtim. önemli kural interface i implemente eden classlardan nesne ürettiği zaman aynı tipte yakalamak yerşne implemente ettiği interface olarak tanımlayanilirtim。 IWork gibi bir tip birden fazla tipmiş gibi davranabilecektir IWork gibi bir tip birden fazla tipmiş gibi davranabilecektir

Syso("Software Developper"); Syso("软件开发者");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM