Michèle S.
Michèle S.

Reputation: 314

jUnit-Testing from another class

I simplified the problem.

I have 3 classes:

  1. My operating class, which has just one static method (add) in it, which should add to ints:
    public class T{
        public static int add(int a, int b){
            return a+b;
        }
    }

I am pretty sure, there is no error in this simple method. Here is my jUnit-Test:

import org.junit.*;
import static org.junit.Assert.*;
import junit.framework.TestCase; 

public class MyJUnitTest extends TestCase{
    T t;
    public boolean test(){
       junit.framework.TestResult t = this.run();
       System.out.println(t.wasSuccessful());
       System.out.println(t.failures.nextElement());
       return t.wasSuccessful();
    }
    public void setUp() throws Exception {
       t = new T();
    }
    public void tearDown() throws Exception {
        t = null;
    }
    //@Test
    public void test1(){
        int[] vgl1 = new int[1];
        int[] vgl2 = new int[1];
        vgl1[0] = t.add(1,2);
        vgl2[0] = 3;
        Assert.assertArrayEquals("Test one", vgl1, vgl2);
    }
}   

Note: I test with Arrays, because in my not simplified Version of the problem, there are Arrays, which I have to test.

Here comes the problem, I cannot understand: If I run the Test in my IDE I get a success (till here, quite understandable), but if I run a third class with one static method:

public class A{
    public static void abc(){
        MyJUnitTest t = new MyJUnitTest();
        t.test();
    }
}

I get a failureCount of one and wasSuccessful returns false. Also System.out.println(t.failures.nextElement()); prints null(MyJUnitTest): TestCase.fName cannot be null.

This is, what I cannot understand. The test should be successful!

Also I tried to use jUnit4, which means, I adapted my MyJUnitTest-Class. But doing so, I cannot call the run-Method. Is there an equivalent in jUnit4? In my IDE I can just click on "test All", but I need to make the Tests in another class an get the information, wheather all Test were successful.

Upvotes: 0

Views: 370

Answers (0)

Related Questions