[JAVA] How to run JUnit in Eclipse

What is JUnit

Prerequisites

ProjectA  ----  src
                 |
                 ------ xx.yy.zzz(package)
                            |
                            ------- Door.java (Tested class)

Configuration

Create a class "DoorTest.java" for testing Door.java (test target class) under package xx.yy.zzz.

image.png

image.png

image.png

DoorTest.java


package xx.yy.zzz;  //Make it the same package as the class under test

import static org.junit.Assert.*;

import org.junit.Test;

public class DoorTest {

	@Test
	public void test() {
		fail("Not yet implemented");
	}

}

image.png

image.png

Write a test program

Tested class

Door.java


package xx.yy.zzz;  //Make it the same package as the test class

public class Door {
	private boolean isOpen = false;

	public void open(String str) {
		if (str.equals("open Sesame!")) {
			isOpen = true;
		} else {
                        isOpen = false;
			System.out.println("wrong.");
		}
	}

	public void close() {
		isOpen = false;
	}

	// getter
	public boolean isOpen() {
		return isOpen;
	}

}

Test content

Test class

Door.java


package xx.yy.zzz;  //Make it the same package as the class under test

import static org.junit.Assert.*;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DoorTest {

	Door door;
    private ByteArrayOutputStream baos;

	//Preprocessing
	@Before
	public void setUp() {
		door = new Door();

        baos = new ByteArrayOutputStream();
        System.setOut(
            new PrintStream(
                new BufferedOutputStream(baos)
            )
        );
	}


	// 「@"Test" is an annotation for JUnit to recognize as a test method.
	//Create with public void
	@Test
	public void test1() {
		door.open("Please open the door");

		//Standard output
		System.out.flush();
		String expected = "wrong.\r\n";
        String actual = baos.toString();

        //Confirmation with expected value
		assertEquals(false, door.isOpen());
        assertEquals(expected, actual);
	}

	@Test
	public void test2() {
		door.open("open Sesame!");
		assertEquals(true, door.isOpen());
	}

	@Test
	public void test3() {
		door.close();
		assertEquals(false, door.isOpen());
	}

	//Post-processing
	@After
	public void fin() {
		// DO Nothing
	}

}

[Supplement]

Recommended Posts

How to run JUnit in Eclipse
How to run Ant in Gradle
How to set Lombok in Eclipse
How to filter JUnit Test in Gradle
How to include Spring Tool in Eclipse 4.6.3?
How to fix system date in JUnit
[For beginners] How to debug in Eclipse
To debug in eclipse
How to use JUnit 5
[JavaFX] How to write Eclipse permissions in build.gradle
How to color code console output in Eclipse
JUnit 5: How to write test cases in enum
How to run a djUnit task in Ant
How to automatically generate a constructor in Eclipse
How to use JUnit (beginner)
How to write Junit 5 organized
How to use Eclipse Debug_Shell
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit
How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
How to use JDD library in Scala with Eclipse
How to Git manage Java EE projects in Eclipse
How to run Java EE Tutial on github on Eclipse
How to debug the generated jar file in Eclipse
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to master programming in 3 months
How to run JavaFX on Docker
How to learn JAVA in 7 days
How to get parameters in Spark
How to set character code and line feed code in Eclipse
How to install Bootstrap in Ruby
How to run npm install on all projects in Lerna
How to use InjectorHolder in OpenAM
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
java Eclipse How to debug javaScript
How to batch run JUnit and get coverage as well
How to concatenate strings in java
How to install Swiper in Rails
How to run a job with docker login in AWS batch
[swift5] How to specify color in hexadecimal
A memorandum on how to use Eclipse
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to use Apache Derby on Eclipse
How to change app name in rails
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to insert a video in Rails
How to standardize header footer in Thymeleaf
How to install Eclipse (Photon) on Mac
How to add jar file in ScalaIDE