Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ClassNotFound Exception

I'm trying to test that a class is not found with UnitTest on Android.

What's going on:
1. I'm writing an android library with transitive dependencies which are resolved in the host application
2. The developer may remove some dependencies for example remove all com.example.package
3. I have a Factory that will try to instantiate (using reflection) an Object and catch the ClassNotFoundException. If the developer remove the dependencies, the exception should be thrown.
4. I want to test this case, but all I found is issue with dependencies, not how to test for it.

Example code I want to test

try {
    sNetworkResponseBuilderClass = OkHttpNetworkResponse.Builder.class;
} catch (Exception e){
    // <<<< I want to test this case
    new ClassNotFoundException("Unable to find OkHttpNetworkResponse.Builder.class").printStackTrace();
    return null;
}

library used: hamcrast, mockito, JUnit 4.

Do you know how to do it?

like image 716
Hugo Gresse Avatar asked Apr 19 '16 09:04

Hugo Gresse


People also ask

What happens if a test method throws an exception?

Yes it is completely fine, and if it does throw the exception the test will be considered as failed. You need to specify that the method throws an Exception even if you know that the specific case does not (this check is done by the compiler).

How do you test an exception case?

To test the exceptions, we should follow the following steps: Create a class to be tested. Create a test case class for testing exceptions. Create a Test Runner class to execute the test case.


2 Answers

So for me the first thing you need to do is to extract the part of the code that can throw a ClassNotFoundException in order to be able to easily mock it, something like:

public Class<? extends NetworkResponseBuilder> getNetworkResponseBuilderClass()
    throws ClassNotFoundException {
    // Your logic here
}

Then you can test a real factory instance using Mockito.spy to be able to redefine the behavior of the method getNetworkResponseBuilderClass() as next:

public void testFactoryIfNetworkResponseBuilderNotFound() {
    Factory factory = spy(new Factory());
    when(factory.getNetworkResponseBuilderClass()).thenThrow(
        new ClassNotFoundException()
    );
    // The rest of your test here
}

public void testFactoryIfNetworkResponseBuilderFound() {
    Factory factory = spy(new Factory());
    when(factory.getNetworkResponseBuilderClass()).thenReturn(
        OkHttpNetworkResponse.Builder.class
    );
    // The rest of your test here
}

More details about Mockito.spy.

like image 142
Nicolas Filotto Avatar answered Oct 14 '22 17:10

Nicolas Filotto


Not quite sure if I understood your question correctly, but you can check with JUnit if an exception gets thrown:

@Test(expected=ClassNotFoundException.class)
public void testClassNotFoundException() {
    // a case where the exception gets thrown
}
like image 35
Christoph Avatar answered Oct 14 '22 16:10

Christoph