Skip to content

Instantly share code, notes, and snippets.

@richard1230
Last active July 29, 2020 08:20
Show Gist options
  • Save richard1230/f9646c53e80f8524d974b330e64ecc5f to your computer and use it in GitHub Desktop.
Save richard1230/f9646c53e80f8524d974b330e64ecc5f to your computer and use it in GitHub Desktop.
一个单元测试所发现的bug

代码地址:

https://github.com/richard1230/wxshop/commit/51d6f0c6e8433ed6fdad5468f3ce742c3ea13052

问题复现步骤:

  1. 开启相关容器:
#启动order模块里面的测试对应的数据库
$docker run -d --name wxshop-order-Test_mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=order mysql

#启动main模块wxshop数据库
docker run -d --name wxshop_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wxshop mysql

#启动main模块测试需要的数据库
docker run -d --name wxshopTest_mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wxshop mysql

#启动order模块主数据库order
$docker exec -it wxshop_mysql mysql -uroot -proot -e 'create database if not exists `order`'

#启动redis
docker run -d -p 6379:6379 --name=myredis redis

#启动测试用的redis
docker run -p 6380:6379 --name myTestRedis -d redis

#启动zookeeper
$docker run -it -p 2181:2181 zookeeper

2.运行如下命令:(mvn clean verify) 会发现报错信息: [ERROR] Failures: [ERROR] ShoppingCartIntegrationTest.canDeleteShoppingCartData:167 expected: <1> but was: <2> [INFO]

$mvn clean verify
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.github:wxshop-api:jar:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: mysql:mysql-connector-java:jar -> duplicate declaration of version (?) @ line 29, column 25
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] wxshop-parent                                                      [pom]
[INFO] wxshop-api                                                         [jar]
[INFO] wxshop-main                                                        [jar]
[INFO] wxshop-order                                                       [jar]
[INFO] 
[INFO] ----------------------< com.github:wxshop-parent >----------------------
[INFO] Building wxshop-parent 0.0.1-SNAPSHOT                              [1/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ wxshop-parent ---
[INFO] 
[INFO] -----------------------< com.github:wxshop-api >------------------------
[INFO] Building wxshop-api 0.0.1-SNAPSHOT                                 [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ wxshop-api ---
[INFO] Deleting /Users/mac/IdeaProjects/wxshop/wxshop-api/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ wxshop-api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/mac/IdeaProjects/wxshop/wxshop-api/src/main/resources
[INFO] skip non existing resourceDirectory /Users/mac/IdeaProjects/wxshop/wxshop-api/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ wxshop-api ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to /Users/mac/IdeaProjects/wxshop/wxshop-api/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ wxshop-api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/mac/IdeaProjects/wxshop/wxshop-api/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ wxshop-api ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ wxshop-api ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ wxshop-api ---
[INFO] Building jar: /Users/mac/IdeaProjects/wxshop/wxshop-api/target/wxshop-api-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] -----------------------< com.github:wxshop-main >-----------------------
[INFO] Building wxshop-main 0.0.1-SNAPSHOT                                [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ wxshop-main ---
[INFO] Deleting /Users/mac/IdeaProjects/wxshop/wxshop-main/target
[INFO] 
[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default-prepare-agent) @ wxshop-main ---
[INFO] argLine set to -javaagent:/Users/mac/.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/Users/mac/IdeaProjects/wxshop/wxshop-main/target/jacoco.exec
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ wxshop-main ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 14 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ wxshop-main ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 40 source files to /Users/mac/IdeaProjects/wxshop/wxshop-main/target/classes
[INFO] 
[INFO] --- maven-checkstyle-plugin:3.1.0:check (compile) @ wxshop-main ---
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ wxshop-main ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ wxshop-main ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 10 source files to /Users/mac/IdeaProjects/wxshop/wxshop-main/target/test-classes
[INFO] /Users/mac/IdeaProjects/wxshop/wxshop-main/src/test/java/com/github/wxshop/service/GoodsServiceTest.java: /Users/mac/IdeaProjects/wxshop/wxshop-main/src/test/java/com/github/wxshop/service/GoodsServiceTest.java使用了未经检查或不安全的操作。
[INFO] /Users/mac/IdeaProjects/wxshop/wxshop-main/src/test/java/com/github/wxshop/service/GoodsServiceTest.java: 有关详细信息, 请使用 -Xlint: 重新编译。
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ wxshop-main ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.github.wxshop.generate.GoodsTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.697 s - in com.github.wxshop.generate.GoodsTest
[INFO] Running com.github.wxshop.service.TelVerificationServiceTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.github.wxshop.service.TelVerificationServiceTest
[INFO] Running com.github.wxshop.service.GoodsIntegrationTest
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/mac/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/mac/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.1/log4j-slf4j-impl-2.13.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
log4j:WARN No appenders could be found for logger (org.apache.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 14.422 s - in com.github.wxshop.service.GoodsIntegrationTest
[INFO] Running com.github.wxshop.service.GoodsServiceTest
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.168 s - in com.github.wxshop.service.GoodsServiceTest
[INFO] Running com.github.wxshop.service.AuthIntegrationTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.487 s - in com.github.wxshop.service.AuthIntegrationTest
[INFO] Running com.github.wxshop.service.OrderIntegrationTest
2020-07-29 14:28:03.903 [http-nio-auto-1-exec-1] ERROR com.github.wxshop.service.OrderService - 扣减库存失败, 商品id: 5,数量:6
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.409 s - in com.github.wxshop.service.OrderIntegrationTest
[INFO] Running com.github.wxshop.service.ShoppingCartIntegrationTest
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.661 s <<< FAILURE! - in com.github.wxshop.service.ShoppingCartIntegrationTest
[ERROR] canDeleteShoppingCartData  Time elapsed: 0.692 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
        at com.github.wxshop.service.ShoppingCartIntegrationTest.canDeleteShoppingCartData(ShoppingCartIntegrationTest.java:167)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   ShoppingCartIntegrationTest.canDeleteShoppingCartData:167 expected: <1> but was: <2>
[INFO] 
[ERROR] Tests run: 30, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for wxshop-parent 0.0.1-SNAPSHOT:
[INFO] 
[INFO] wxshop-parent ...................................... SUCCESS [  0.217 s]
[INFO] wxshop-api ......................................... SUCCESS [  2.132 s]
[INFO] wxshop-main ........................................ FAILURE [ 34.024 s]
[INFO] wxshop-order ....................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  36.680 s
[INFO] Finished at: 2020-07-29T14:28:11+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project wxshop-main: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/mac/IdeaProjects/wxshop/wxshop-main/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :wxshop-main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment