# Problem with JUNIT

• 04-11-2014, 12:03 PM
peliasrojo
Problem with JUNIT
Hi, i've made a class in java (eclipse) and i've tested it in Junit. The class shows if a number is palindrome, perfect and also calculates the combinacional of two numbers (fact(x)/(fact(y)*fact(x-y))) and is in this case when i've the problem cause it always returns 0.

I've tested the class without Junit and it works ok, so i don't know what's wrong.

The class code is:
Code:

```public class Calculador {                 //-------------------------------------------------------------------------------         //Returns true o false if the number is or not a perfect number         //-------------------------------------------------------------------------------                 public boolean esPerfecto(int x){                 int suma=0; /*Variable auxiliar*/                                 for (int contador=1; contador<x;contador++){                         if (x % contador ==0)                                         suma=suma+contador;}                 if (suma==x){ //Si la suma de los divisores coincide con el número --> Es perfecto/                                                 return true;}                 else{                                                 return false;}                 }                         //-----------------------------------------------------------------------------------------         // Returns the combinacional (m sobre n) supposed correct the numbers. Here's when i have the problem         //-----------------------------------------------------------------------------------------                         public long combinatorio(int x,int y){                 long combina;                                 combina=factorial(x)/(factorial(y)*factorial(x-y));                                                                 return combina;        }                 //----------------------------------------------------------------------------------------         // Returns Factorial         //-----------------------------------------------------------------------------------------                         public static long factorial (int x){                 long fact=1;                 for (int i=1;i<=x;i++){                         fact=fact*i;                 }                                 return fact;}                        //------------------------------------------------------------------------------         // Returns true o false if the number is or not a palindrome         //-------------------------------------------------------------------------------                         public boolean esCapicua (long w){                 long k,s,r;                 s=0;                 k=w;                 while (w!=0){                         r=w%10;                         s=r+s*10; // S almacena el número invertido                         w=w/10;        }                 if(s==k)                         return true;                 else                         return false;}         }```
And the Junit code is:

Code:

```import junit.framework.TestCase; public class CalculadorTest extends TestCase {         //-------------------------------------------         //-------------------------------------------         public CalculadorTest(String name) {                 super(name);         }         //-------------------------------------------         //-------------------------------------------                 //-------------------------------------------         //-------------------------------------------         protected void setUp() throws Exception {}         //-------------------------------------------         //-------------------------------------------                 //-------------------------------------------         //-------------------------------------------         protected void tearDown() throws Exception {}         //-------------------------------------------         //-------------------------------------------                 //-------------------------------------------         //--------PRUEBAS DE JUNIT-------------------         //-------------------------------------------                 //-------------------------------------------         //--------COMPROBACIÓN SI 5 ES PERFECTO------         //-------------------------------------------         public void testesPerfecto\$5(){                 Calculador a = new Calculador();                 assertEquals(a.esPerfecto(5),false);}                 //-------------------------------------------         //--------COMPROBACIÓN SI 6 ES PERFECTO------         //-------------------------------------------         public void testesPerfecto\$6(){                 Calculador a = new Calculador();                 assertEquals(a.esPerfecto(6),true);}                         //-------------------------------------------         //--------COMPROBACIÓN SI 28 ES PERFECTO------         //-------------------------------------------         public void testesPerfecto\$28(){                 Calculador a = new Calculador();                 assertEquals(a.esPerfecto(28),true);}                         //-------------------------------------------         //--------COMPROBACIÓN SI 55 ES CAPICUA------         //-------------------------------------------         public void testescapicua\$55(){                 Calculador a = new Calculador();                 assertEquals(a.esCapicua(55),true);}                                 //-------------------------------------------         //--------COMPROBACIÓN SI 65 ES CAPICUA------         //-------------------------------------------         public void testescapicua\$65(){                 Calculador a = new Calculador();                 assertEquals(a.esCapicua(65),true);} //Error 65 no es capicúa.                 //-------------------------------------------         //--------COMPROBACIÓN SI 282 ES CAPICUA------         //-------------------------------------------                 public void testescapicua\$282(){                         Calculador a = new Calculador();                         assertEquals(a.esCapicua(282),true);}                 //-------------------------------------------         //--------COMPROBACIÓN del combinatorio 2,3------         //-------------------------------------------                 public void testescombinatorio\$2\$3(){                         Calculador a = new Calculador();                         assertEquals(a.combinatorio(2, 3),8);} //Error. Resultado es 1                //-------------------------------------------         //--------COMPROBACIÓN del combinatorio 2,3------         //-------------------------------------------                 public void testescombinatorios\$2\$3(){                                 Calculador a = new Calculador();                                 assertEquals(a.combinatorio(2, 3),1);}                 }```
Well I hope you can help me. Thanks, Peliasrojo.
• 04-11-2014, 12:37 PM
gimbal2
Re: Problem with JUNIT
So use a debugger to step through your code or put some System.out.println() statements to print out the values in your code.

There is no magic going on here, if the code behaves correctly in your unit tests then the problem is likely in how the code is used in the real application.
• 04-11-2014, 01:09 PM
Tolls
Re: Problem with JUNIT
I assume (from the comments) this is one of the steps going wrong?
Code:

`assertEquals(a.esCapicua(65),true);`
When I run the code you give then I get "false". So it's nothing to do with JUnit. Your code is giving the same result (not surprisingly) each time.
• 04-11-2014, 01:58 PM
peliasrojo
Re: Problem with JUNIT
Hi, the problem was with the code:

Code:

` assertEquals(a.combinatorio(2, 3),8);`
This should return the value of (3!/(2!*(3-2)!). Now i've noticed that with this code the program tries to calculate a negative factorial (2-3)! so the correct code should be:

Code:

` assertEquals(a.combinatorio(3, 2),8);`
Here the result is 3. I've put 8 to see how the errors are showed.

With this change is all ok.

Thanks. Regards peliasrojo.