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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • 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
  • JUnit, 4, 5, Jupiter, Vintage

Trending

  • How to Introduce a New API Quickly Using Micronaut
  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  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.5K 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

  • 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
  • JUnit, 4, 5, Jupiter, Vintage

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!