Results 1 to 4 of 4
  1. #1
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default 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;}
    	
    }
    And the Junit code is:

    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);} 
    	
    	}
    Well I hope you can help me. Thanks, Peliasrojo.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default 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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

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

Similar Threads

  1. Problem with JUnit classpath
    By annnDi in forum New To Java
    Replies: 0
    Last Post: 12-29-2012, 08:40 PM
  2. problem with JUnit test
    By exltus in forum Advanced Java
    Replies: 12
    Last Post: 12-19-2011, 07:49 PM
  3. JUnit problem
    By SeanC in forum New To Java
    Replies: 0
    Last Post: 01-13-2011, 01:11 AM
  4. JUnit 4 / JUNIT_HOME Problem
    By albertkao in forum Eclipse
    Replies: 0
    Last Post: 11-22-2010, 09:43 PM
  5. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 09:41 PM

Posting Permissions

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