我是Java的新手,并且正在关注Eclipse Total Beginner的教程.它们都非常有用,但是在第12课中,他使用assertTrue
了一个测试用例和assertFalse
另一个测试用例.这是代码:
// Check the book out to p1 (Thomas)
// Check to see that the book was successfully checked out to p1 (Thomas)
assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message
assertEquals("Thomas", b1.getPerson().getName());
assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message
assertEquals("Book was already checked out", m1.checkOut(b1,p2));
Run Code Online (Sandbox Code Playgroud)
我搜索了这些方法的好文档,但没有找到任何东西.如果我的理解是正确的,assertTrue
那么assertFalse
当第二个参数求值为false时显示字符串.如果是这样的话,拥有它们的重点是什么?
编辑:我想我看到令我困惑的是什么.作者可能只是为了展示他们的功能而放置它们(毕竟它是一个教程).然后他设置了一个会失败的信息,这样就会打印出来并告诉我为什么失败了.开始变得更有意义......我认为这是解释,但我不确定.
我使用Jfunc构建了现有的框架,即使在测试用例中的一个断言失败时也提供了继续执行的工具.Jfunc使用junit 3.x框架.但是现在我们正在迁移到junit4,所以我不能再使用Jfunc并用junit 4.10 jar替换它.
现在的问题是因为我们在框架中广泛使用了jfunc,而对于junit 4,我们希望使代码继续执行,即使其中一个断言在测试用例中失败.
有没有人对此有任何建议/想法,我知道在junit中测试需要更加原子化,即每个测试用例一个断言,但由于某种原因我们不能在我们的框架中这样做.
我试图在我的Linux命令提示符上运行JUnit /opt/junit/
包含必要的JARS(hamcrest-core-1.3.jar和junit.jar)和类文件,我使用以下命令来运行JUnit:
java -cp hamcrest-core-1.3.jar:junit.jar:. org.junit.runner.JUnitCore TestRunner
Run Code Online (Sandbox Code Playgroud)
TestJunit类:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
@Test
public void testAdd() {
String str= "Junit is working fine";
assertEquals("Junit is working fine",str);
}
}
Run Code Online (Sandbox Code Playgroud)
TestRunner的:
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println("fail ho gaya"+failure.toString());
}
System.out.println("passed:"+result.wasSuccessful());
}
}
Run Code Online (Sandbox Code Playgroud)
我在运行它时遇到以下异常
JUnit version 4.11
.E
Time: 0.003
There was 1 failure: …
Run Code Online (Sandbox Code Playgroud) 我在运行以下代码时遇到问题:
configService.setMainConfig("src/test/resources/MainConfig.xml");
Run Code Online (Sandbox Code Playgroud)
来自Junit @Before方法.
这是Maven构建其目标文件夹的方式吗?
在单元测试中计算方法调用的最佳方法是什么.任何测试框架都允许这样做吗?
当我运行我的单元测试时,它会调用我的计划任务.我想防止这种行为,这是由我@EnableScheduling
在主应用程序配置上的事实引起的.
如何在单元测试中禁用此功能?
我遇到过这个问题/答案,建议设置个人资料?
不确定我会怎么做?或者如果它有点矫枉过正?我正在考虑为我的单元测试使用单独的AppConfiguration,但是当我这样做时,感觉就像是重复代码两次?
@Configuration
@EnableJpaRepositories(AppConfiguration.DAO_PACKAGE)
@EnableTransactionManagement
@EnableScheduling
@ComponentScan({AppConfiguration.SERVICE_PACKAGE,
AppConfiguration.DAO_PACKAGE,
AppConfiguration.CLIENT_PACKAGE,
AppConfiguration.SCHEDULE_PACKAGE})
public class AppConfiguration {
static final String MAIN_PACKAGE = "com.etc.app-name";
static final String DAO_PACKAGE = "com.etc.app-name.dao";
private static final String ENTITIES_PACKAGE = "com.etc.app-name.entity";
static final String SERVICE_PACKAGE = "com.etc.app-name.service";
static final String CLIENT_PACKAGE = "com.etc.app-name.client";
static final String SCHEDULE_PACKAGE = "com.etc.app-name.scheduling";
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
// stripped code for question readability
}
// more app config code below etc
}
Run Code Online (Sandbox Code Playgroud)
单元测试示例.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AppConfiguration.class})
@Transactional …
Run Code Online (Sandbox Code Playgroud) 我有一个使用lombok的基于Gradle的项目.我已将此项目导入IntelliJ IDEA 14.1(使用导入外部模型导入方法).我可以在Gradle中没有问题地运行JUnit4单元测试,但IntelliJ似乎在看到Lombok生成的Getters时遇到了问题.这使我无法在IDEA中运行测试.
为了确保它不是一个设置问题,我创建了一个非常简单的项目,并确认在简单的测试项目中出现了同样的问题.
我的版本:Gradle:gradle-2.3-all通过Gradle包装器
Intellij IDEA:IU-141.713
Lombok插件:0.9.2
我在这里错过了什么?
更新通过看不出我的意思是身体不可见.我是说这个:
/home/meeee/workspace/junit-lombok-idea/lib/src/main/java/com/example/jumbokea/Main.java
Error:(10, 11) java: cannot find symbol
symbol: method setIntField(int)
location: variable sc of type com.example.jumbokea.SomeClass
Error:(12, 41) java: cannot find symbol
symbol: method getIntField()
location: variable sc of type com.example.jumbokea.SomeClass
Error:(14, 33) java: constructor AnotherClass in class com.example.jumbokea.AnotherClass cannot be applied to given types;
required: no arguments
found: float,com.example.jumbokea.SomeClass
reason: actual and formal argument lists differ in length
Error:(16, 46) java: cannot …
Run Code Online (Sandbox Code Playgroud) NoSuchMethodError
JUnit和Hamcrest组合的另一个例子.违规代码:
assertThat(dirReader.document(0).getFields(), hasItem(
new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") {
@Override
protected String featureValueOf(IndexableField actual) {
return actual.name();
} } ));
Run Code Online (Sandbox Code Playgroud)
IndexerTest.java中的注释行152-157 (commit ac72ce)
导致NoSuchMethodError(请参阅http://db.tt/qkkkTE78以获取完整输出):
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.FeatureMatcher.matchesSafely(FeatureMatcher.java:43)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:25)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.junit.Assert.assertThat(Assert.java:770)
at org.junit.Assert.assertThat(Assert.java:736)
at indexer.IndexerTest.testIndexContainsField(IndexerTest.java:152)
Run Code Online (Sandbox Code Playgroud)
A NoSuchMethodError
是由调用非现有方法的(已编译)类引起的.describeMismatch
JUnit + Hamcrest组合的特定情况通常是由JUnit中包含的Hamcrest类与Hamcrest库中的那些类的版本之间的不兼容引起的.
聚甲醛包含Hamcrest库1.3,Hamcrest芯1.3,和JUnit 4.11的显式依赖,(按顺序)所建议加勒特厅在答复以获取:在运行测试时,"的NoSuchMethodError org.hamcrest.Matcher.describeMismatch" IntelliJ …
我正在将我的应用程序迁移到androidx,我似乎无法让我的单元测试工作.我从Google的AndroidJunitRunnerSample中获取了示例,该示例已更新为使用新的androidx api.尝试运行测试时出现以下错误:
java.lang.Exception: Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded. Check your build configuration.
Run Code Online (Sandbox Code Playgroud)
这是我的模块build.gradle:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
// Test dependencies
androidTestImplementation 'androidx.test:core:1.0.0-beta02'
androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-beta02'
androidTestImplementation "androidx.arch.core:core-testing:2.0.0"
androidTestImplementation 'androidx.room:room-testing:2.1.0-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-beta02'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}
Run Code Online (Sandbox Code Playgroud)
以下是我的测试结构:
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@RunWith(AndroidJUnit4.class)
public class EntityParcelTest {
@BeforeClass
public void createEntities() {
// Setup...
}
@Test
void someTest() {
// Testing here
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?