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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • JUnit 5 Tutorial: Nice and Easy [Video]

Trending

  • Agile Estimation: Techniques and Tips for Success
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • API Design
  • Freedom to Code on Low-Code Platforms
  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

Upender Chinthala user avatar by
Upender Chinthala
·
Mar. 17, 15 · Interview
Like (1)
Save
Tweet
Share
32.00K 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

  • Readability in the Test: Exploring the JUnitParams
  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • JUnit 5 Tutorial: Nice and Easy [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: