10.1.3 account-captcha的测试代码

测试代码位于src/test/java目录,其包名也与主代码一致,为com.juvenxu.mvnbook.account.captcha。首先看一下简单的RandomeGeneratorTest,见代码清单10-10。

代码清单10-10 RandomeGeneratorTest.java


package com.juvenxu.mvnbook.account.captcha;

import static org.junit.Assert.assertFalse;

import java.util.HashSet;

import java.util.Set;

import org.junit.Test;

public class RandomGeneratorTest

{

@Test

public void testGetRandomString()

throws Exception

{

Set<String>randoms=new HashSet<String>(100);

for(int i=0;i<100;i++)

{

String random=RandomGenerator.getRandomString();

assertFalse(randoms.contains(random));

randoms.add(random);

}

}

}


该测试用例创建一个初始容量为100的集合randoms,然后循环100次用Random-Generator生成随机字符串并放入randoms中,同时每次循环都检查新生成的随机值是否已经包含在集合中。这样一个简单的检查能基本确定RandomGenerator生成值是否为随机的。

当然这个模块中最重要的测试应该在AccountCaptchaService上,见代码清单10-11。

代码清单10-11 AccountCaptchaServiceTest.java


package com.juvenxu.mvnbook.account.captcha;

import static org.junit.Assert.*;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import org.junit.Before;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AccountCaptchaServiceTest

{

private AccountCaptchaService service;

@Before

public void prepare()

throws Exception

{

ApplicationContext ctx=new ClassPathXmlApplicationContext("account-

captcha.xml");

service=(AccountCaptchaService)ctx.getBean("accountCaptchaService");

}

@Test

public void testGenerateCaptcha()

throws Exception

{

String captchaKey=service.generateCaptchaKey();

assertNotNull(captchaKey);

byte[]captchaImage=service.generateCaptchaImage(captchaKey);

assertTrue(captchaImage.length>0);

File image=new File("target/"+captchaKey+".jpg");

OutputStream output=null;

try

{

output=new FileOutputStream(image);

output.write(captchaImage);

}

finally

{

if(output!=null)

{

output.close();

}

}

assertTrue(image.exists()&&image.length()>0);

}

@Test

public void testValidateCaptchaCorrect()

throws Exception

{

List<String>preDefinedTexts=new ArrayList<String>();

preDefinedTexts.add("12345");

preDefinedTexts.add("abcde");

service.setPreDefinedTexts(preDefinedTexts);

String captchaKey=service.generateCaptchaKey();

service.generateCaptchaImage(captchaKey);

assertTrue(service.validateCaptcha(captchaKey,"12345"));

captchaKey=service.generateCaptchaKey();

service.generateCaptchaImage(captchaKey);

assertTrue(service.validateCaptcha(captchaKey,"abcde"));

}

@Test

public void testValidateCaptchaIncorrect()

throws Exception

{

List<String>preDefinedTexts=new ArrayList<String>();

preDefinedTexts.add("12345");

service.setPreDefinedTexts(preDefinedTexts);

String captchaKey=service.generateCaptchaKey();

service.generateCaptchaImage(captchaKey);

assertFalse(service.validateCaptcha(captchaKey,"67890"));

}

}


该测试类的prepare()方法使用@Before标注,在运行每个测试方法之前初始化AccountCaptchaService这个bean。

testGenerateCaptcha()用来测试验证码图片的生成。首先它获取一个验证码主键并检查其非空,然后使用该主键获得验证码图片,实际上是一个字节数组,并检查该字节数组的内容非空。紧接着该测试方法在项目的target目录下创建一个名为验证码主键的jpg格式文件,并将AccountCaptchaService返回的验证码图片字节数组内容写入到该jpg文件中,然后再检查文件存在且包含实际内容。运行该测试之后,就能在项目的target目录下找到一个名如dhb022fc.jpg的文件,打开是一个验证码图片,如图10-1所示。

10.1.3 account-captcha的测试代码 - 图1

图 10-1 AccountCaptchaServiceTest生成的验证码图片

testValidateCaptchaCorrect()用来测试一个正确的Captcha验证流程。它首先预定义了两个Captcha的值放到服务中,然后依次生成验证码主键、验证码图片,并且使用主键和已知的值进行验证,确保服务正常工作。

最后的testValidateCaptchaIncorrect()方法测试当用户反馈的Captcha值错误时发生的情景,它先预定义Captcha的值为“12345”,但最后验证是传入了“67890”,并检查validateCaptcha()方法返回的值为false。

现在运行测试,在项目目录下运行mvn test,就会得到如下输出:


[INFO] Scanning for projects…

[INFO]

[INFO] -------------------------------------------------------------------

[INFO] Building Account Captcha 1.0.0 - SNAPSHOT

[INFO] -------------------------------------------------------------------

[INFO] …

[INFO]

[INFO] ---maven-surefire-plugin:2.4.3:test (default-test) @ account-captcha ---

[INFO] Surefire report directory: D:\code\ch -10 \account-aggregator\account-captcha

\target\surefire-reports

------------------------------------------------------------------------

T E S T S

------------------------------------------------------------------------

Running com.juvenxu.mvnbook.account.captcha.RandomGeneratorTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec

Running com.juvenxu.mvnbook.account.captcha.AccountCaptchaServiceTest

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.016 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[INFO] -------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] -------------------------------------------------------------------


这个简单的报告告诉我们,Maven运行了两个测试类,其中第一个测试类RandomGen-eratorTest包含1个测试,第二个测试类AccountCaptchaServiceTest包含3个测试,所有4个测试运行完毕后,没有任何失败和错误,也没有跳过任何测试。

报告中的Failures、Errors、Skipped信息来源于JUnit测试框架。Failures(失败)表示要测试的结果与预期值不一致,例如测试代码期望返回值为true,但实际为false;Errors(错误)表示测试代码或产品代码发生了未预期的错误,例如产品代码抛出了一个空指针错误,该错误又没有被测试代码捕捉到;Skipped表示那些被标记为忽略的测试方法,在JUnit中用户可以使用@Ignore注解标记忽略测试方法。