java org.junit_JUnit-java 单元测试

本文介绍了如何使用JUnit进行Java单元测试,包括测试的基本概念、简单的测试用例编写,以及如何利用setUp()和tearDown()方法简化测试代码。还提到了JUnit生成测试类的插件,以及Mockito框架在单元测试中的应用。
摘要由CSDN通过智能技术生成

JUnit

JUnit is a simple framework to write repeatable tests.

It is an instance of the xUnit architecture for unit testing frameworks.

What to test?

Need

Desc

Right

结果是否正确

B

边界条件是否满足

I

能反向关联吗

C

有其他手段交叉检查吗

E

是否可以强制异常发生

P

性能问题

Simple Demo

We create a test class for student;

public class StudentTest extends TestCase {

public void testCreate() {

Student student = new Student("Mike");

}

}

Student class

public class Student {

private String name;

public Student(String name) {

this.name = name;

}

public String getName() {

return "ryo";

}

public void setName(String name) {

this.name = name;

}

}

when we run StudentTest.

2016-04-27-success-junit.png

then, we change the test code.

public class StudentTest extends TestCase {

public void testCreate() {

Student student = new Student("Mike");

String name = student.getName();

assertEquals("Mike", name);

}

}

result

2016-04-27-failed-junit.png

Usage

Add jars in IDEA

File --> Project Structure [crtl+alt+shift+s] --> Libraries --> "+"---> "Attach Files or Directories"

setUp()

Now we add a new class Course.

public class Course {

private String name;

private int num;

public Course(String name, int num) {

this.name = name;

this.num = num;

}

public String getName() {

return name;

}

public int getNum() {

return num;

}

}

test class like this…

public class CourseTest extends TestCase {

public void testCreateNum() {

Course course = new Course("Math", 1);

assertEquals(1, course.getNum());

}

public void testCreateName() {

Course course = new Course("Math", 1);

assertEquals("Helo", course.getName());

}

}

You may find

Course course = new Course("Math", 1);

we have write it twice, can it be easier?

Now, we can use setUp() to help us to do it easier; things in setUp() will be called before each test method.

public class CourseTest extends TestCase {

private Course course;

public void setUp() {

course = new Course("Math", 1);

}

public void testCreateNum() {

assertEquals(1, course.getNum());

}

public void testCreateName() {

assertEquals("Helo", course.getName());

}

}

tearDown()

Also, tearDown() will be called after each test method.

@Before

Method annotated with @Before executed before every test; also, @After after…

@BeforeClass

Just run one time, and is unique.

JUnitGenerator

This plugin generates JUnit tests from right click ‘Generate…’ menu while focused on a java class.

The unit test output code can be customized using a provided velocity template to format the code based on the origin class.

If a unit test is created where one already exists, the user is prompted for overwrite or merge operation.

The merge operation allows the user to selectively create the target file content.

2016-08-30-junit.png

set the output path

${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}

set the junit4 template

########################################################################################

##

## Available variables:

## $entryList.methodList - List of method composites

## $entryList.privateMethodList - List of private method composites

## $entryList.fieldList - ArrayList of class scope field names

## $entryList.className - class name

## $entryList.packageName - package name

## $today - Todays date in MM/dd/yyyy format

##

## MethodComposite variables:

## $method.name - Method Name

## $method.signature - Full method signature in String form

## $method.reflectionCode - list of strings representing commented out reflection code to access method (Private Methods)

## $method.paramNames - List of Strings representing the method's parameters' names

## $method.paramClasses - List of Strings representing the method's parameters' classes

##

## You can configure the output class name using "testClass" variable below.

## Here are some examples:

## Test${entry.ClassName} - will produce TestSomeClass

## ${entry.className}Test - will produce SomeClassTest

##

########################################################################################

##

#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end

## Iterate through the list and generate testcase for every entry.

#foreach ($entry in $entryList)

#set( $testClass="${entry.className}Test")

##

package $entry.packageName;

import org.junit.Test;

import org.junit.Before;

import org.junit.After;

/**

* ${entry.className} Tester.

*

* @author houbinbin

* @since $today

* @version 1.0

*/

public class $testClass {

@Before

public void before() throws Exception {

}

@After

public void after() throws Exception {

}

#foreach($method in $entry.methodList)

/**

*

* Method: $method.signature

*

*/

@Test

public void ${method.name}Test() throws Exception {

}

#end

#foreach($method in $entry.privateMethodList)

/**

*

* Method: $method.signature

*

*/

@Test

public void ${method.name}Test() throws Exception {

#foreach($string in $method.reflectionCode)

$string

#end

}

#end

}

#end

test template with Mockito

########################################################################################

##

## Available variables:

## $entryList.methodList - List of method composites

## $entryList.privateMethodList - List of private method composites

## $entryList.fieldList - ArrayList of class scope field names

## $entryList.className - class name

## $entryList.packageName - package name

## $today - Todays date in MM/dd/yyyy format

##

## MethodComposite variables:

## $method.name - Method Name

## $method.signature - Full method signature in String form

## $method.reflectionCode - list of strings representing commented out reflection code to access method (Private Methods)

## $method.paramNames - List of Strings representing the method's parameters' names

## $method.paramClasses - List of Strings representing the method's parameters' classes

##

## You can configure the output class name using "testClass" variable below.

## Here are some examples:

## Test${entry.ClassName} - will produce TestSomeClass

## ${entry.className}Test - will produce SomeClassTest

##

########################################################################################

##

#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end

#macro (uncap $strIn)$strIn.valueOf($strIn.charAt(0)).toLowerCase()$strIn.substring(1)#end

## Iterate through the list and generate testcase for every entry.

#foreach ($entry in $entryList)

#set( $testClass="${entry.className}Test")

##

package $entry.packageName;

import org.junit.Test;

import org.junit.Before;

import org.mockito.InjectMocks;

import org.mockito.MockitoAnnotations;

/**

* ${entry.className} Tester.

*

* @author houbinbin

* @since $today

* @version 1.0

*/

public class $testClass {

@InjectMocks

private ${entry.className} #uncap(${entry.className});

@Before

public void init() {

MockitoAnnotations.initMocks(this);

}

#foreach($method in $entry.methodList)

/**

*

* Method: $method.signature

*

*/

@Test

public void ${method.name}Test() throws Exception {

}

#end

#foreach($method in $entry.privateMethodList)

/**

*

* Method: $method.signature

*

*/

@Test

public void ${method.name}Test() throws Exception {

#foreach($string in $method.reflectionCode)

$string

#end

}

#end

}

#end

Mockito

Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API.

Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors.

If you want to test classA as following, you need create class BCD at first.

classA->classB:

classB->classC:

classB->classD:

When you use mock, things will be like

classA->classBMock:

Hello World

maven jar

org.mockito

mockito-all

1.8.4

hello world

@Test

public void testMock() {

List mockedList = mock(List.class);

// stubbing appears before the actual execution

when(mockedList.get(0)).thenReturn("hello");

String result = mockedList.get(0);

//verify has called get(0)

verify(mockedList).get(0);

assertEquals("hello", result);

}

Mock demo

public class UserServiceTest {

@InjectMocks

private UserService userService;

@Mock

private UserDao userDao;

@Before

public void init(){

MockitoAnnotations.initMocks(this);

//mock the method

User user = new User();

Mockito.when(this.UserDao.selectUser(Mockito.anyString()))

.thenReturn(user);

}

@Test

public void testGetUser() {

}

}

PowerMock

https://github.com/jayway/powermock

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值