junit测试抛出404找不到预期值

cx6n0qe3  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(304)

我有这个问题,因为一个星期以来,我没有找到足够的资源。我有一个使用外部api的java应用程序,我想用wiremock测试我的应用程序。当我想发送一个文件,它总是显示404找不到。我发现问题是wiremock没有保留主机头的值,所以下面有这段代码,但它总是不起作用。我尝试了真正的api,它使用wiremock记录和回放功能运行得非常好,但是我指定了-preserve host header选项(没有这个选项,它将得到404 not found)我要测试的服务(我没有控制器):

@Service
public class Bank{
    //This method is working
    public String hello(String hello){
        return exernalAPI.get("hello").toString(); // It's just an example
    }
    //This method is not working in test(404)
    public void uploadFile(String inputStrum...){
       //The code here is used to send the file to an external API using method post
    }
}

我做了一个记录和回放来捕捉请求和来自真实外部api的响应,
等级测试:

@AutoConfigureWireMock(port=0)
@ContextConfiguration(classes={Bank.class})
public class BankTest{

@Autowired
public Bank bank;
@Test //The test is passing
public void helloTest(){
    String expected = resourse.getResource("classpath:response/helloMessage.txt").toString(); //Get the captured response
    String actual = bank.hello("Jack"); //this return Hi Jack and it's working
    assertEquals(expected,actual);
}
@Test //This test is not passing
public String uploadFileTest(){
     InputSteam is = getInputStream();
     //code to get inputStream 
     Resource expected= resourse.getResourse("classpath:responses/upload_response.json");
     String expected = expected.toString();

     is.setContent(resource.getResource("classpath:file_to_upload.zip"));

     String actual = bank.uploadFile(is); // Here i have 404 not found
     assertEquals(expected,actual);

}

after several searches i found that i have to preserve the Host header using somthing like this :
preserveHostHeader(true);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题