1. Member
Join Date
Apr 2014
Posts
7
Rep Power
0

## 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:
Java 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*/

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:

Java Code:
```import junit.framework.TestCase;

public class CalculadorTest extends TestCase {

//-------------------------------------------
//-------------------------------------------
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(){
assertEquals(a.esPerfecto(5),false);}

//-------------------------------------------
//--------COMPROBACIÓN SI 6 ES PERFECTO------
//-------------------------------------------
public void testesPerfecto\$6(){
assertEquals(a.esPerfecto(6),true);}

//-------------------------------------------
//--------COMPROBACIÓN SI 28 ES PERFECTO------
//-------------------------------------------
public void testesPerfecto\$28(){
assertEquals(a.esPerfecto(28),true);}

//-------------------------------------------
//--------COMPROBACIÓN SI 55 ES CAPICUA------
//-------------------------------------------
public void testescapicua\$55(){
assertEquals(a.esCapicua(55),true);}

//-------------------------------------------
//--------COMPROBACIÓN SI 65 ES CAPICUA------
//-------------------------------------------
public void testescapicua\$65(){
assertEquals(a.esCapicua(65),true);} //Error 65 no es capicúa.

//-------------------------------------------
//--------COMPROBACIÓN SI 282 ES CAPICUA------
//-------------------------------------------
public void testescapicua\$282(){
assertEquals(a.esCapicua(282),true);}

//-------------------------------------------
//--------COMPROBACIÓN del combinatorio 2,3------
//-------------------------------------------
public void testescombinatorio\$2\$3(){
assertEquals(a.combinatorio(2, 3),8);} //Error. Resultado es 1
//-------------------------------------------
//--------COMPROBACIÓN del combinatorio 2,3------
//-------------------------------------------
public void testescombinatorios\$2\$3(){
assertEquals(a.combinatorio(2, 3),1);}

}```
Well I hope you can help me. Thanks, Peliasrojo.

2. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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.

3. Moderator
Join Date
Apr 2009
Posts
13,369
Rep Power
24

## Re: Problem with JUNIT

I assume (from the comments) this is one of the steps going wrong?
Java 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.

4. Member
Join Date
Apr 2014
Posts
7
Rep Power
0

## Re: Problem with JUNIT

Hi, the problem was with the code:

Java 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:

Java 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•