1

I am doing a Junit test and in one of the tests I have problems but I don't understand why.

You do not have to allow more objects to be added to the list at any given time, that the size of the list is greater than 5, in short, if I add the sixth object to the list, the exception must be skipped, but I do not understand the reason why It does not work, I leave the code of what I am doing.

Code:

 @Test
 void testAddTank() throws KeeperException, TankException {  
  List<Tank> NumTanques = new ArrayList<Tank>();

  Tank tank1 = new Tank("TANQUE1","pedro1","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank2 = new Tank("TANQUE2","pedro2","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank3 = new Tank("TANQUE3","pedro3","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank4 = new Tank("TANQUE4","pedro4","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank5 = new Tank("TANQUE5","pedro5","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank6 = new Tank("TANQUE6","pedro6","hola",3.0,2.0,1.0,"/image",17.0,7);
  Tank tank7 = new Tank("TANQUE7","pedro6","hola",3.0,2.0,1.0,"/image",17.0,7);

  NumTanques.add(tank1);
  NumTanques.add(tank2);
  NumTanques.add(tank3);
  NumTanques.add(tank4);
  NumTanques.add(tank5);

  assertEquals(5, NumTanques.size());

  KeeperException excepcionTanques = assertThrows(KeeperException.class,()->NumTanques.add(tank6));
  assertEquals("[ERROR] A keeper cannot care of more than 5 
  thanks!!",excepcionTanques.MSG_ERR_MAX_TANKS);

}

As you can see, I create the list of type tank and create some tank objects, which I add to the list. Then with the assert what I tell you is that 5 has to be the size of the list so that it returns true ...

Then in the exception that I create I add a new tank to the list and the exception should jump, but it does not, it exits and gives an error on the line:

 KeeperException excepcionTanques = assertThrows(KeeperException.class,()->NumTanques.add(tank6));

I do not see clearly what is happening.

Progman
  • 16,827
  • 6
  • 33
  • 48

3 Answers3

4

I highly doubt that the Java class ArrayList throws an exception as you described it:

List<Tank> NumTanques = new ArrayList<Tank>();

So if you want that Exception to be thrown you might need to use a custom class for that that keeps track of the added tanks and not use a plain ArrayList for that. Perhaps start by finding the code that actually throws an KeeperException.

Smutje
  • 17,733
  • 4
  • 24
  • 41
1

list.add only throws exceptions as mentioned here https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(E).

So it is expected that line mentioned in question will give you compilation error.

If you still want to assert on list.add, then use any of exceptions mentioned in java doc.

may be something like this --

UnsupportedOperationException e= assertThrows(UnsupportedOperationException.class,()->NumTanques.add(tank6));
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
0

Why Arraylist will throw and exception if you add anything to it. It has its own algorithm to manage addition.

What you need to do is add a method in your service class (maybe KeeperService.java)

And throw exception there. Something like

@sneakyThrows
public void countKeeperCheck(Object o){

List<Tank> NumTanques = new ArrayList<Tank>();
NumTanques.add(o);

if(listOfBanks.size() >5){
throw new KeeperException("write whatever here")
}
}

//add to try/catch if you have specific things to handle

And you can call that method from your test. You cant write business logic in your test. From tests you call methods that holds your business logic.

----------------Edit After Comments and detailed code-----------------

public void addTank(List<Tank> tank) throws KeeperException{// in the comment you add wrong signature, method should accept List, the only you will have contains operation

        if(tanks.size()>=5){
        throw new KeeperException(KeeperException.MSG_ERR_MAX_TANKS);
        }

        if(!tanks.contains(tank)){
            tanks.add(tank);
    }
}


// and this is how your test will look like
@Test
 void testAddTank() throws KeeperException, TankException {
         List<Tank> NumTanques = new ArrayList<Tank>();

        Tank tank1 = new Tank("TANQUE1","pedro1","hola",3.0,2.0,1.0,"/image",17.0,7);
        Tank tank2 = new Tank("TANQUE2","pedro2","hola",3.0,2.0,1.0,"/image",17.0,7);
        Tank tank3 = new Tank("TANQUE3","pedro3","hola",3.0,2.0,1.0,"/image",17.0,7);
        Tank tank4 = new Tank("TANQUE4","pedro4","hola",3.0,2.0,1.0,"/image",17.0,7);
        Tank tank5 = new Tank("TANQUE5","pedro5","hola",3.0,2.0,1.0,"/image",17.0,7);
        Tank tank6 = new Tank("TANQUE6","pedro6","hola",3.0,2.0,1.0,"/image",17.0,7);

        NumTanques.add(tank1);
        NumTanques.add(tank2);
        NumTanques.add(tank3);
        NumTanques.add(tank4);
        NumTanques.add(tank5);


        KeeperException excepcionTanques = assertThrows(KeeperException.class,()->yourServiceClass.addTank(tank6));
        assertEquals("[ERROR] A keeper cannot care of more than 5  thanks!!",excepcionTanques.MSG_ERR_MAX_TANKS);
        }

declare YourServiceClass the way you want.

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
  • In my keeper class I already control that in the addtank method, but I don't know how to do the test with junit. In the keeper class the addtank method I did it like this: public void addTank (Tank tank) throws KeeperException { if (tanks.contains (tank)) { tank = null; } if ((tanks.size () <= 5) && (tank! = null)) { tanks.add (tank); if (tanks.size ()> 5) { throw new KeeperException (KeeperException.MSG_ERR_MAX_TANKS); } } – Pedro martinez Jun 01 '20 at 10:19
  • First rule of coding 'Never Complicate'. I have refactored your method. Please reuse. Always add details in first time in your question. – Kunal Vohra Jun 01 '20 at 11:08
  • How should this work? You still add 5 tanks to the `List` and only once call `addTank` and also with the wrong parameters so that doesn't even compile? – Smutje Jun 01 '20 at 13:13