Hugo Gresse
Hugo Gresse

Reputation: 17909

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?

Upvotes: 19

Views: 7301

Answers (7)

CrawlingKid
CrawlingKid

Reputation: 994

OkHttpNetworkResponse.Builder might be as follows:

package com.example.model;

public class OkHttpNetworkResponse {
    public static class Builder {

    }
}
  1. 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.

Factory Class: which will create any object might be as follows:

package com.example.factory;

public class Factory {
    public static Object getInstance(String className)
            throws ClassNotFoundException, InstantiationException,
            IllegalAccessException {
        Class clazz = Class.forName(className);
        return clazz.newInstance();
    }
}
  1. The developer may remove some dependencies for example remove all com.example.package
  2. I want to test this case, but all I found is issue with dependencies, not how to test for it.

FactoryTest Class: which will test whether ClassNotFoundException is thrown or not might be as follows: N.B: please Check the comments carefully.

package com.example.factory;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

public class FactoryTest {
    Factory factory;

    @Test(expected=ClassNotFoundException.class)
    public void test() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        ClassLoader loader = FactoryTest.class.getClassLoader();
        String directory = loader.getResource(".").getPath() + "/com/example/model";
        File dir = new File(directory);
        //Checking directory already existed or not..
        assertTrue("Directory:"+dir.getPath()+" not exist",dir.exists());
          //Deleting directory 
        deleteDirectoryProgramatically(directory);
        //Checking directory already deleted or not..
        assertFalse("Directory:"+dir.getPath()+" still exist",dir.exists());
        //Now getInstance Method will throw ClassNotFoundException because OkHttpNetworkResponse.Builder.class has been deleted programatically. 
        Factory.getInstance("OkHttpNetworkResponse.Builder.class");
    }

    private void deleteDirectoryProgramatically(String directory) {
        File dir = new File(directory);
        System.out.println(dir.getAbsolutePath());
        String[] files = dir.list();
        for (String f : files) {
            File fl = new File(directory,f);
            System.out.println(f+ " deleted?"+fl.delete());
        }
        System.out.println(dir+ " deleted?"+dir.delete());
    }
}

Upvotes: 3

Luis
Luis

Reputation: 3571

Use Class.forName("com.example.ClassName")

try {
 Class.forName("com.example.OkHttpNetworkResponse.Builder");
} catch (ClassNotFoundException e) {
  // This class was not found
}

See Class.forName(String className)

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

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.

Upvotes: 10

SkyWalker
SkyWalker

Reputation: 29168

It is very simple issue. JUnit4 exception unit testing is given below with an example. Hope it will clarify you.

MyNumber.java

public class MyNumber {
   int number;
   public MyNumber div(MyNumber rhs) {
      if (rhs.number == 0) throw new IllegalArgumentException("Cannot divide by 0!");
      this.number /= rhs.number;
      return this;
   }
}

MyNumberTest.java

public class MyNumberTest {
   private MyNumber number1, number2; // Test fixtures
   @Test(expected = IllegalArgumentException.class)
   public void testDivByZero() {
      System.out.println("Run @Test testDivByZero"); // for illustration
      number2.setNumber(0);
      number1.div(number2);
   }
}

JUnit - Exceptions Test

To test if the code throws a desired exception, use annotation @Test(expected = exception.class), as illustrated in the previous example. For your case it will be

/**
* Check for class not found exception
**/
@Test(expected=ClassNotFoundException.class)
public void testClassNotFoundException() {
    .....
}

For better understanding, you can go through this tutorial: Java Unit Testing - JUnit & TestNG. It contains full running code example step by step with explanation.

Upvotes: 2

Idrish Multani
Idrish Multani

Reputation: 141

it is your dictionary problem. in your dictionary in test class will not have . change your dictionary.

Upvotes: 0

Pankaj Verma
Pankaj Verma

Reputation: 191

inside catch you can check the object with the instanceof operator as :

try {
    sNetworkResponseBuilderClass = OkHttpNetworkResponse.Builder.class;
} catch (Exception e){
    if(e instanceof ClassNotFoundException){
        // here you can do the code you want in case of ClassNotFoundException thrown
    }
}

Upvotes: 0

Christoph
Christoph

Reputation: 93

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
}

Upvotes: 6

Related Questions