Doubt with Unit Tests Junit

1

I'm doing some unit tests with Junit and I have a doubt to do an exercise.

When the test class is created, test cases are created for each method. Now, if you want to try only one method ... is it convenient to eliminate the rest of the tests that have been created for each method ?, or should they stay as they are all?

Also, if I have a class instantiated in the main class with its initialized attributes with exact values (for example: amount of money in an account among others), when performing the test of a "withdrawMoney" method, the Test class is supposed to have that attribute initialized in the main class to recalculate the balance, no? Or should I start a variable in the method test so that the test can be executed?

Hello, I have another question, I continue with the unit tests and I do not understand what I have wrong in the following code:

@Test
public double testIngresar() throws Exception {       
    System.out.println("ingresar");
    CCuenta instance = new CCuenta();
    double resultado = instance.ingresar(100.5);
    assertEquals(2600.5, resultado);
    System.out.print(resultado);
}

The error that comes out of "incompatible types, void can not be converted to double" in the line where I declare the result variable I do not understand because I get it. The enter () method is declared in the CClase class as double too, I have a small mess of concepts as you can see ...

    
asked by rodic 17.01.2017 в 21:35
source

1 answer

1

If you want to work with unit tests it is recommended to implement tests for all methods. If the method is trivial, the test is also trivial, so do not waste a lot of time. If at any time you have to maintain a method that was trivial to start and you get an error, you will be grateful for the tests implemented.

When you create tests with variable data, access to BD or other more complex activities you will have to put together a context for the tests, in some cases you should create a "mock" of the real component that uses the same interface.

The subject is very broad, so if you have concrete cases it is easier to provide sample code.

    
answered by 17.01.2017 в 23:35