[JAVA] Unit test with Junit.

environment

・ Windows10 ・ Jdk 1.8.0_201 -Eclipse Version: 2018-12 (4.10.0) ・ Junit_4.12.0

Target

Being able to run unit tests for classes created in java.

What is Junit

Java version of xUnit testing framework for created classes You can test the operation by calling the method of the class under test from the created Junit code.

Since the test code created once can be reused, compared to the method of creating test code every time The man-hours required for testing can be reduced.

Precautions for testing with Junit

  1. Must be a public method.
  2. The return value is void.
  3. It has no arguments.
  4. Annotate the test method with org.junit.Test.

Create a class to be tested.

This time, I created a class Calculator.java that performs multiplication and division.

Calculator.java


package junit.tutorial;

public class Calculator {

	/**
	 *Returns the result of multiplying x and y
	 * @param x
	 * @param y
	 * @return Returns the result of multiplying x and y
	 */
	public int maltiplication(int x,int y) {
		return x * y;
	}

	/**
	 *Returns the division result of x and y
	 * @param x
	 * @param y
	 * @return Returns the division result of x and y
	 */
	public int division(int x,int y) {
		return x / y;
	}
}

Create test code.

Next, create a test class. If you create a new class, enter test and then ctrl + space Eclipse Content Assist will start, so select the template for Junit 4 You can easily create a method.

First, create a test class CalculatorTest class with nothing in it.

CalculatorTest.java


package junit.tutorial;

public class CalculatorTest {
}

Insert the template method here using the above content assist.

CalculatorTest.java


package junit.tutorial;

import static org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest {
	@Test
	public void testName() throws Exception {
		
	}
}

Describe in the test code.

This time, we will create the test code as follows. Finally, by confirming the result of 3, it can be judged whether the test worked as expected.

  1. Set the input value used in the method.
  2. Set the expected value of the return value of the method.
  3. Assert the expected value and the measured value, which is the return value of 1.

Complete the test code.

Complete the test code immediately. The method name is Japanese. This allows you to see at a glance what the execution result has verified.

Calculator.java


package junit.tutorial;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest {

	@Test
You can get the multiplication result of 5 and 2 with public void maltiplication() throws Exception {
		Calculator sut = new Calculator();
		int expected = 10;
		int actual = sut.maltiplication(5, 2);
		assertThat(actual,is(expected));
	}

	@Test
You can get the division result of 5 and 8 by public void division() throws Exception {
		Calculator sut = new Calculator();
		int expected = 
		
	}
}

While setting the expected value of the test code, I notice that the return value is not good as a result of division if it is int. In response to this fact, the class under test is modified.

Calculator.java


package junit.tutorial;

public class Calculator {

	/**
	 *Returns the result of multiplying x and y
	 * @param x
	 * @param y
	 * @return Returns the result of multiplying x and y
	 */
	public int maltiplication(int x,int y) {
		return x * y;
	}

	/**
	 *Returns the division result of x and y
	 * @param x
	 * @param y
	 * @return Returns the division result of x and y
	 */
	public float division(int x,int y) {
		return (float)x / (float)y;
	}
}

I set the return type to float and cast the return value. Now back to test code creation.

CalculatorTest.java


package junit.tutorial;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest {

	@Test
You can get the multiplication result of 5 and 2 with public void maltiplication() throws Exception {
		Calculator sut = new Calculator();
		int expected = 10;
		int actual = sut.maltiplication(5, 2);
		assertThat(actual,is(expected));
	}

	@Test
You can get the division result of 5 and 8 by public void division() throws Exception {
		Calculator sut = new Calculator();
		float expected = 0.625f;
		float actual =  sut.division(5, 8);
		assertThat(actual,is(expected));

	}
}

You can now run the test. Now, let's run a test as Junit in Eclipse and check the result.

image1.png

-All indicators are green on the screen. -The execution result must be completed in 2/2.

From the above results, it was found that the method worked as expected.

This is the basic operation of Junit.

I would also like to study the outline of the exam using Junit.

References

[Introduction to JUnit practice ── Unit test technique to learn systematically WEB + DB PRESS plus] https://www.amazon.co.jp/dp/B07JL78S95/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1

Recommended Posts

Unit test with Junit.
Test Web API with junit
WebAPI unit test and integration test with SpringBoot + Junit5, 4 patterns
[Java] Test private methods with JUnit
Test Spring framework controller with Junit
Let's unit test with [rails] Rspec!
JUnit unit test pre-processing and post-processing order
JUnit 5 gradle test fails with lombok annotation
Java automated test implementation with JUnit 5 + Gradle
[Java] How to test for null with JUnit
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Integration Test with Gradle
How to test interrupts during Thread.sleep with JUnit
Easy JUnit test of Elasticsearch 2018 version with embedded-elasticsearch
Java Unit Test Library-Artery-Sample
[Rails] Test with RSpec
Test code using mock with JUnit (EasyMock center)
Automatically test with Gauge
Load test with JMeter
Mixin test cases with JUnit 5 and default methods
How to unit test with JVM with source using RxAndroid
Test the contents of an Excel file with JUnit
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
I wrote a test with Spring Boot + JUnit 5 now
[Java] JUnit4 test case example
JUnit 5 fails with java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute
[IntelliJ IDEA] Perform Unit Test
Test Active Strage with RSpec
Introduction to Micronaut 2 ~ Unit test ~
Unit test architecture using ArchUnit
Remote debugging with Gradle test
Test GraphQL resolver with rspec
Test private methods in JUnit
Implementation of unit test code
[JUnit] Test the thrown exception
UnitTest with SpringBoot + JUnit + Mockito
Java unit tests with Mockito
Use Spring Test + Mockito + JUnit 4 for Spring Boot + Spring Retry unit tests
[Java] I want to test standard input & standard output with JUnit
test
How to unit test Spring AOP
[Book Review] Unit test of programming site that can be done with zero experience (sequel 1-JUnit ~)
REST API test with REST Assured Part 2
Test with RSpec + Capybara + selenium + chromedriver
JUnit 5 parameterization test is super convenient
test
Output test coverage with clover + gradle
Sample code to unit test a Spring Boot controller with MockMvc
[Rails5] Rspec -Unit test when nesting-
test
About app testing RSpec (unit test)
test
junit
Output JUnit test report in Maven
Significance of existence of unit test (self-discussion)
Copy and paste test with RSpec
Java Unit Test Library-Artery / JUnit4-Array Equivalence
Test list inclusion relationships with AssertJ
[RSpec] Unit test (using gem: factory_bot)
How to test private methods with arrays or variadic arguments in JUnit