Results 1 to 4 of 4
Thread: Problem with JUNIT
- 04-11-2014, 01:03 PM #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*/ 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;} }
Java 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);} }
- 04-11-2014, 01:37 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 04-11-2014, 02:09 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Problem with JUNIT
I assume (from the comments) this is one of the steps going wrong?
Java Code:assertEquals(a.esCapicua(65),true);
Please do not ask for code as refusal often offends.
** This space for rent **
- 04-11-2014, 02:58 PM #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);
Java Code:assertEquals(a.combinatorio(3, 2),8);
With this change is all ok.
Thanks. Regards peliasrojo.
Similar Threads
-
Problem with JUnit classpath
By annnDi in forum New To JavaReplies: 0Last Post: 12-29-2012, 09:40 PM -
problem with JUnit test
By exltus in forum Advanced JavaReplies: 12Last Post: 12-19-2011, 08:49 PM -
JUnit problem
By SeanC in forum New To JavaReplies: 0Last Post: 01-13-2011, 02:11 AM -
JUnit 4 / JUNIT_HOME Problem
By albertkao in forum EclipseReplies: 0Last Post: 11-22-2010, 10:43 PM -
junit test problem
By moamen in forum EclipseReplies: 2Last Post: 03-14-2010, 10:41 PM
Bookmarks