안드로이드

안드로이드 junit test / android retrofit junit test

뇽꾸리 2022. 3. 7. 17:02
반응형

우선 디펜던시에 추가는 다해주는게 좋다 해노면 편함 

디펜던시에 추가 

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

 

public class HttpCallTestService {

    private static final String TAG = "CallService";
    Gson gson = new GsonBuilder().setLenient().create();
    private Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("8080") //base url 
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    private HttpService httpService = retrofit.create(HttpService.class);


    /**
     * 통계자료 업로드
     * @param
     * @author me
     */
    @Test
    public void uploadStatTest()throws Exception{
        JSONObject joReq = new JSONObject();
        joReq.put("uploadId", 12);
        String body = joReq.toString();
        httpService.uploadStat(body).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    Log.d(TAG, "onResponse: " + response.toString());
                    Log.d(TAG ,"onResponse body : " + response.body().toString());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.e(TAG, "onFailure: 통신 실패 " + t);
            }
        });
    }
}

 

public interface HttpService {

    /**
     * 통계자료 업로드
     * @param
     * @author me
     */
    @Headers({"Content-Type: application/json"})
    @POST("/user/me")
    Call<String> uploadStat(@Body String body);

}

 

반응형