JUnit - SPLessons

JUnit Integration Test

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JUnit Integration Test

JUnit Integration Test

shape Introduction

This chapter demonstrate about JUnit Integration Test. For integration testing user need to use databases or other resources which is also known as testing from the integration level. Following are the concepts covered in this chapter.

  • Integration Testing
  • SMS Notifier

Integration Testing

shape Description

JUnit also useful integration level testing.To do these testing make sure that SUT is using Dependencies because user need to test everything work together and can make use of JUnit to do the integration tests. The image below demonstrate the integration testing.

shape Step 1

In order to test JUnit Integration Test level, JUnit will use the Google voice account to generate the SMSNotifier which generates the Name, Email id and Number in the form of Google voice mail in order to do this the code is below demonstrate the JUnit Integration testing..

import static org.junit.asserttrue;

public class IntegrationTest {
	
	private Voice voice;
	
	@Before 
	public void setup() throws IOException{
		voice = new Voice("splessons@gmail.com", "splessions123");
	}

	@Test
	public void GoalMetShouldSendNotification() throws IOException, InvalidGoalException
	{
		TrackingService service = new TrackingService(new SMSNotifier("solessons"."splessons123"));
		service.setGoal(50);
		service.addProtein(51);
		
		assertTrue(voice.getSMS().contains("goal met"));
	}
	@After
	public void tearDown() throws IOException {
		for(SMSThread thread : voice.getSMSThreads());
		{
			//voice.deleteMessaqe(thread.getId());
		}
	}
}

shape Step 2

By running the above generated code a window get appears Google voice inbox provided with Message, Name and Email the below code demonstrate clearing the message.

import static org.junit.asserttrue;

public class IntegrationTest {
	
	private Voice voice;
	
	@Before 
	public void setup() throws IOException{
		voice = new Voice("splessons@gmail.com", "splessions123");
	}

	@Test
	public void GoalMetShouldSendNotification() throws IOException, InvalidGoalException
	{
		TrackingService service = new TrackingService(new SMSNotifier("solessons"."splessons123"));
		service.setGoal(50);
		service.addProtein(51);
		
		assertTrue(voice.getSMS().contains("goal met"));
	}
	@After
	public void tearDown() throws IOException {
		for(SMSThread thread : voice.getSMSThreads());
		{
			voice.deleteMessaqe(thread.getId());
		}
	}
}

For injtegration level of testing JUnit will use the databases or available resources.

SMS Notifier

shape Description

The user can create an integration test for tracking service by creating a real implementation for the notifier interface. By which implementation will create a send notification through SMS when goal is reached. The Java library used to check and send SMS through the Google voice. The test code will call the tracking service to send the SMS notifier, now test code will call Google voice itself to verify the message by itself and The test code have another part to clean the messages as shown in below image. 

shape Examples

The below code demonstrate the SMS Notifier which implements Notifier interface as shown in the code below.

package com.simpleprogrammer.proteinTtracker;

import java.io.IOExceptions;

public class SMSNotifier implements Notifier{
	
	private String userName;
	private String password;
	private String numberTomessage;
	
	public SMSNotifier(String userName, String password, String numberToMessage){
		this.userName = userName;
		this.password = password;
		this.numberToMessage = numberToMessage;
	}
	@Override
	public boolean send(String message){
		try{
			voice voice = new voice(userName, password);
			voice.sendSMS(numberToMessage, message);
		}catch (IOException e){
			return false;
		}
		return true;
	}
}

Summary

shape Key Points

  • JUnit Integration Test use Java voice to generate the SMS.
  • SMS notifier will use Java libraries.
  • JUnit Integration Test will performs many levels of testings.