Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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;
}

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}