DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introduction to Data-Driven Testing With JUnit 5: A Guide to Efficient and Scalable Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java

Trending

  • [closed] DZone's 2025 Developer Community Survey
  • AI in Software Development: A Mirror, Not a Magic Wand
  • Reactive Kafka With Spring Boot
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  1. DZone
  2. Coding
  3. Java
  4. Multiple JUNIT Asserts Can Combine Into One Single Assert By Using Builder

Multiple JUNIT Asserts Can Combine Into One Single Assert By Using Builder

By 
Upender Chinthala user avatar
Upender Chinthala
·
Mar. 17, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
33.8K Views

Join the DZone community and get the full member experience.

Join For Free

Problem 1:  Multiple Asserts

Using multiple asserts are not good practice because if first one fail and the remaining asserts will not reach example:   
Assert.assertEquals("Field1", mock.field1);
Assert.assertEquals(expectedField2, mock.field2);
Assert.assertEquals(expectedField3, mock.field3);
Assert.assertEquals(expectedField4, mock.field4);

Problem 2: Single Assert with && operator condition 

Problem 1 can achieve by combining multiple conditions by using && operator but the issue is to difficult know which one is failed.   

Assert.assertTrue("Field1".equals(mock.field1) 
&& expectedField2==mock.field2
&& expectedField3==mock.field3 && expectedField4==mock.field4);
Solution: by creating simple builder class can address the above two issues.    in this example add method has third argument i.e label and it will tell whenever assertion  failed in particular condition. Example: The below JUNIT code will fail because expected "Field2" but we got "Field1"   

The assertion failure message show like this, java.lang.AssertionError: expected:<[Field2]> but was <[Field1]> failed at Field1  
EqualsBuilder eqb = EqualsBuilder.newBuilder()
.and("Field2",mock.field1,"Field1").and(expectedField2, mock.field2,"Field2")
.and(expectedField3, mock.field3,"Field3").and(expectedField4, mock.field4,"Field4"); 
Assert.assertTrue(eqb.getMessage(),eqb.result());
complete code is here. EqualsBuilder.java
 
package com.demo;

import java.text.MessageFormat;

/**
* @author UpenderC
*
*/
public class EqualsBuilder {
private boolean result = true;
private String text="";
public static EqualsBuilder newBuilder() {
return new EqualsBuilder();
}
/**
* @param expected
* @param actual
* @param msg
* @return
* example:
*/
public EqualsBuilder and(final Object expected,final Object actual, final String msg) {
result = result && actual!=null && expected!=null ? expected.equals(actual):false;
if (!result && text.length()<1) {
text = MessageFormat.format("expected:<[{0}]> but was <[{1}]> failed at {2}",expected,actual,msg);
}
return this;
}
public boolean result() {
return result;
}
public String getMessage() {
return text;
}
}
MultipleAssertsTest.java  
package com.stewi.demo;

import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class MultipleAssertsTest {

@Test
public void multipleAsserts() {
Date expectedField4 = new Date();
Integer expectedField2 = 1;
Long expectedField3 =2000000000l;
MockFields mock = getMock(1);

/*example1:
Assert.assertEquals("Field1", mock.field1);
Assert.assertEquals(expectedField2, mock.field2);
Assert.assertEquals(expectedField3, mock.field3);
Assert.assertEquals(expectedField4, mock.field4);*/

/* example2:
* Assert.assertTrue("Field1".equals(mock.field1) 
&& expectedField2==mock.field2
&& expectedField3==mock.field3 && expectedField4==mock.field4);
*/

//example3:
EqualsBuilder eqb = EqualsBuilder.newBuilder()
.and("Field2",mock.field1,"Field1").and(expectedField2, mock.field2,"Field2")
.and(expectedField3, mock.field3,"Field3").and(expectedField4, mock.field4,"Field4");
Assert.assertTrue(eqb.getMessage(),eqb.result());

}

private MockFields getMock(int scenario) {
switch(scenario) {
case 1:
MockFields iMock1 = new MockFields();
iMock1.field1="Field1";
iMock1.field2=1;
iMock1.field3=2000000000l;
iMock1.field4=new Date();
return iMock1;
case 2:
MockFields iMock2 = new MockFields();
return iMock2;
default:
return null;
}
}

}
/** 
* just created mock , in real time this class may generated by 
* third party and doesn't have equals method to compare complete
* object
* 
*/
class MockFields {
public String field1;
public Integer field2;
public Long field3;
public Date field4;
}
JUnit

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Data-Driven Testing With JUnit 5: A Guide to Efficient and Scalable Testing
  • Why Testing is a Long-Term Investment for Software Engineers
  • TestNG vs. JUnit: A Comparative Analysis of Java Testing Frameworks
  • Mastering Unit Testing and Test-Driven Development in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook