简体   繁体   中英

Unit test case for Mimetype in android

i am newly writing the test cases in android, i want to write test case on get Mimetype from url. Working fine in app mode, but not working in test cases, i tried different ways but i didn't get test case pass, please tell where i did the mistake. Any one can help me thanks in advance.

Here is my code: MainActivity.java

    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        String url = "www.toyota.com/content/ebrochure/2018/avalon_ebrochure.pdf";
        getContentType(pdf);
        }

        // get mimetype from the url
        public String getContentType(String url) {
            String type = "";
            String extension = MimeTypeMap.getFileExtensionFromUrl(url);
            if (extension != null) {
                type =MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
            }
            return type;
        }
}

MainActivityTest.java

import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class MainActivityTest {
    String url = "www.toyota.com/content/ebrochure/2018/avalon_ebrochure.pdf";
    @Test
    public void getContentType(){
        String result = (new MainActivity()).getContentType(url);
        assertEquals("application/pdf", result);
    }
}

Output:

junit.framework.ComparisonFailure: 
Expected :application/pdf
Actual   :

If you're using Robolectric to simulate an Android environment when running your tests, you need to register the media types yourself. By default, the simulated MimeTypeMap is empty.

shadowOf(MimeTypeMap.getSingleton()).addExtensionMimeTypMapping("pdf", "application/pdf");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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