11

I have a DTO which I'm populating from the request object, and the request object has many fields. I want to write a test to check if the populateDTO() method is putting values in the right places or not. If I follow the rule of one assert per test, I would have to write a large number of tests, to test each field. The other approach would be to write multiple asserts in a single test. Is it really recommended to follow one assert per test rule or can we relax in these cases. How do I approach this problem?

7 Answers 7

8

Keep them separate. A unit test is supposed to tell you which unit failed. Keeping them separate also allows you to isolate the problem quickly w/o requiring you to go through a lengthy debug cycle.

5
  • Broken links - please amend and let me know Dec 6, 2013 at 23:46
  • Still broken links. This is why it's better to inline your answer and use links for support than provide links as the main portion of your answer.
    – Jason
    Jan 9, 2014 at 13:46
  • Fixed broken links. Sorry for inconvenience.
    – Nilesh
    Jan 10, 2014 at 8:46
  • Still broken, Nilesh. Maybe convert article to a PDF and link from Google Drive/etc.? Aug 15, 2017 at 21:41
  • I would disagree with you on this one, as JUnit allows you to go to the assertion that failed, so having even 20 assertions one after the other doesn't make any difference, you are probably just adding processing time if you write a function to test every single member of a class on its own. One assertion per test is a myth in my opinion and really overrated / overgeneralised "recommendation". But that's just my opinion.
    – gEdringer
    Jun 12, 2018 at 13:56
6

Is it really recommended to have only one assert per unit test? Yes it is, there are people who make that recommendation. Are they right? I don't think so. I find it hard to believe such people have actually worked on real code for a long time.

So, imangine you have a mutator method you want to unit test. The mutator has some kind of effect, or effects, which you want to check. Typically the expected effect of a mutator are few in number, because many effects suggests an overly complicated design for the mutator. With one assert per effect and one test case per assert, you will not need many test cases per mutator, so the recommendation does not seem so bad.

But the flaw in this reasoning is that those tests are looking at only the expected effects of the mutator. But if the mutator has a bug in it, it might have unexpected faulty side effects. The tests are making the foolish assumption that the code does not have a whole class of bugs, and that no future refactoring will introduce such bugs. When the method was originally written it might be obvious to the author that particular side effects were impossible, but refactoring and addition of new functionality might make such side effects possible.

The only safe way to test long lived code is to check that the mutators do not have unexpected side effects. But how can you test for those? Most classes have some invariants: things that no mutator can ever change. The size method of a container will never return a negative value, for example. Each invariant is, in effect, a post condition for every mutator (and also the constructor). Each mutator also typically has a set of invariants that describe what kind of changes it does not make. A sort method does not change the length of the container, for example. The class and mutator invariants are, in effect, post conditions for every mutator call. Adding assertions for all them is the only way of checking for unexpected side effects.

So, just add more test cases? In practice the number of invariants multiplied by the number of mutators to test is large, so one assertion per test leads to many test cases. And the information about your invariants is scattered over many test cases. A design change to tweak one invariant will require alteration of many test cases. It becomes impractical. Its better to have parameterised test cases for a mutator, which check several invariants for the mutator, using several assertions.

And the authors of JUnit5 seem to agree. They provide an assertAll for checking several assertions in one test-case.

3

this construction help you to have 1 big assert (with small asserts inside)

import static org.junit.jupiter.api.Assertions.assertAll;

assertAll(
    () -> assertThat(actual1, is(expected)),
    () -> assertThat(actual2, is(expected))
);
1

You can have a parameterized test where the 1st parameter is the propertyname and the second the expected value.

0

Is that rule extended to being in a loop? Consider this

Collection expectedValues = // populate expected values
populateDTO();
for(DTO dto : myDtoContainer) 
  assert_equal(dto, expectedValues.get(someIndexRelatedToDto))

Now I'm not so big on the exact syntax, but this is just the notion I'm looking at.

EDIT: After the comments...

The answer is ... Nope!

The reason the principle exists is so you can identify which parts of the object fail. If you have them in one method, you're going to run into only one assertion, then the next, then the next, and you won't see them all.

So you can have it one of two ways:

  1. One method, less boilerplate code.
  2. Many methods, better reporting on the test run

It's up to you, both have ups and downs. 3. List item

3
  • The DTO is POJO..and the request is also a POJO. So the assertes will most likely look like assertEqual("abc", dto.getName()); etc..
    – Ravi
    Feb 17, 2011 at 5:43
  • So you want to avoid just having assert(dto.equals(mockObject), is that what you're saying? You want to assert each, individual element, without violating the 1-per principle?
    – corsiKa
    Feb 17, 2011 at 5:46
  • Yeah, I want to assert each, individual element, without violating the 1-per principle. Just that you know, the request is a JAXB generated object.
    – Ravi
    Feb 17, 2011 at 6:00
0

[caveat: I'm very "unfluent" in Java/JUnit, so beware of errors in the details below]

There's a couple of ways to do this:

1) Write multiple assertions in the same test. This should be ok if you are only testing the DTO generation once. You could start here, and move to another solution when this starts to hurt.

2) Write a helper assertion, e.g. assertDtoFieldsEqual, passing in the expected and actual DTO. Inside the helper assertion you assert each field separately. This at least gives you the illusion of only one assert per test and will make things clearer if you test DTO generation for multiple scenarios.

3) Implement equals for the object that check each property and implement toString so that you at least can inspect the assertion result manually to find out what part is incorrect.

4) For each scenario where the DTO is generated, create a separate test fixture that generates the DTO and initializes the expected properties in the setUp method. The create a separate test for testing each of the properties. This also results in a lot of tests, but they will at least be one-liners only. Example in pseudo-code:

public class WithDtoGeneratedFromXxx : TestFixture
{
  DTO dto = null;

  public void setUp()
  {
    dto = GenerateDtoFromXxx();
    expectedProp1 = "";
    ...
  }

  void testProp1IsGeneratedCorrectly()
  {
    assertEqual(expectedProp1, dto.prop1);
  }
  ...
}

If you need to test the DTO generation under different scenarios and choose this last method it could soon become tedious to write all those tests. If this is the case you could implement an abstract base fixture that leaves out the details on how to create the DTO and setup the expected properties to derived classes. Pseudo-code:

abstract class AbstractDtoTest : TestFixture
{
  DTO dto;
  SomeType expectedProp1;

  abstract DTO createDto();
  abstract SomeType getExpectedProp1();

  void setUp()
  {
    dto = createDto();
    ...
  }

  void testProp1IsGeneratedCorrectly()
  {
    assertEqual(getExpectedProp1(), dto.prop1);
  }
  ...
}


class WithDtoGeneratedFromXxx : AbstractDtoTest
{
  DTO createDto() { return GenerateDtoFromXxx(); }
  abstract SomeType getExpectedProp1() { return new SomeType(); }
  ...
}
1
  • If you want a function to assert all the property fields, you might want to look into the hamcrest class SamePropertyValuesAs. You can use that in Assert.assertThat(returnedDto, samePropertyValuesAs(expectedDto)). (This still gives you only 1 error per test, even when more fields are incorrect.) Mar 15, 2016 at 12:49
-1

Or you can do some workaround.

import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class NewEmptyJUnitTest {

    public NewEmptyJUnitTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }


     @Test
     public void checkMultipleValues() {
         String errMessages = new String();

         try{
             this.checkProperty1("someActualResult", "someExpectedResult");
         } catch (Exception e){
             errMessages += e.getMessage();
         }

        try{
            this.checkProperty2("someActualResult", "someExpectedResult");
         } catch (Exception e){
             errMessages += e.getMessage();
         }

        try{
             this.checkProperty3("someActualResult", "someExpectedResult");
         } catch (Exception e){
             errMessages += e.getMessage();
         }

        Assert.assertTrue(errMessages, errMessages.isEmpty());


     }



     private boolean checkProperty1(String propertyValue, String expectedvalue) throws Exception{
         if(propertyValue == expectedvalue){
             return true;
         }else {
             throw new Exception("Property1 has value: " + propertyValue + ", expected: " + expectedvalue);
         }
     }

       private boolean checkProperty2(String propertyValue, String expectedvalue) throws Exception{
         if(propertyValue == expectedvalue){
             return true;
         }else {
             throw new Exception("Property2 has value: " + propertyValue + ", expected: " + expectedvalue);
         }
     }

         private boolean checkProperty3(String propertyValue, String expectedvalue) throws Exception{
         if(propertyValue == expectedvalue){
             return true;
         }else {
             throw new Exception("Property3 has value: " + propertyValue + ", expected: " + expectedvalue);
         }
     }  
}  

Maybe not the best approach and if overused than can confuse... but it is a possibility.

2
  • I'd say any use is overuse. All this boilerplate just to get around the 'single assert per test'? Sep 28, 2012 at 12:20
  • @merryprankster Well yes, if you don't have tool that does this for you, than you write this on your own. Once you find using this more and more you make this general utility. At some point maybe you add this to your test framework: github.com/KentBeck/junit/blob/r4.8.2/src/main/java/org/junit/…
    – yoosiba
    Nov 22, 2012 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.