JQu*_*ile 56 java junit intellij-idea
我继承了一个Java项目,并且是Java开发的新手.我觉得让代码感觉舒服的好方法就是围绕它编写一些测试.我正在使用IntelliJ编写代码.
我现有的项目有一个像这样的文件夹结构:
/myProject
/src
/main
/java
/com.lexcorp
/core
/email
/providers
emailProvider.java
Run Code Online (Sandbox Code Playgroud)
我创建了一个新项目,将为该项目进行测试.我希望这个项目能够同时进行单元测试和集成测试.目前,我的新项目有这样的结构:
/myProjectTests
/src
/main
/java
/com.lexcorp.core.email.providers
emailProviderTest.java
Run Code Online (Sandbox Code Playgroud)
emailProviderTest.java文件如下所示:
package com.lexcorp.core.email.providers;
import junit.framework.TestCase;
import org.junit.Test;
public class EmailProviderTest extends TestCase {
private final String username = "[testAccount]";
private final String password = "[testPassword]";
@Test
public void thisAlwaysPasses() {
assertTrue(true);
}
}
Run Code Online (Sandbox Code Playgroud)
该项目具有Run/Debug配置,具有以下属性:
当我运行此配置时,我收到一条错误消息:
junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到一个归结为"未找到测试"的错误.虽然我的项目结构不同,但操作系统上的文件夹结构匹配(这是让我感到困惑的另一件事).为什么我会收到此错误,如何解决?非常感谢!
小智 124
我也得到了错误
junit.framework.AssertionFailedError:在...中找不到测试
但我忘了说明
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Run Code Online (Sandbox Code Playgroud)
在同步项目后,它找到了测试.所以也许它可以帮助别人
Rei*_*eus 33
扩展junit.framework.TestCase
是实现测试用例的旧JUnit 3方法,因为没有方法从字母测试开始,所以不起作用.由于您使用的是JUnit 4,因此只需将该类声明为
public class EmailProviderTest {
Run Code Online (Sandbox Code Playgroud)
并且可以从@Test
注释中找到测试方法.
阅读:JUnit混乱:使用'extend Testcase'或'@Test'?
lid*_*dox 13
我也得到了这个:
junit.framework.AssertionFailedError:在...中找不到测试
通过从method1重命名测试方法来解决
@Test
public void method1(){
// Do some stuff...
}
Run Code Online (Sandbox Code Playgroud)
to testMethod1
@Test
public void testMethod1(){
// Do some stuff...
}
Run Code Online (Sandbox Code Playgroud)
对于 Java 框架,我在 Intellij IDEA 中遇到了同样的问题。我做得很好:
但是我做错了一件事:方法的名称没有以 "test" 开头,实际上是:
@Test
public void getSomethingTest(){
Run Code Online (Sandbox Code Playgroud)
当我把它改成:
@Test
public void testGetSomethingTest(){
Run Code Online (Sandbox Code Playgroud)
我已经解决了:执行者终于能够将此方法识别为测试方法。 我没有改变其他任何东西。
归档时间: |
|
查看次数: |
53109 次 |
最近记录: |