草庐IT

Junit-Tests

全部标签

java.lang.AssertionError : Content type not set while junit Spring MVC Controller? 错误

我正在使用JUnit来测试我的SpringMVCController。下面是我的方法,它返回一个index.jsp页面并在屏幕上显示HelloWorld-@RequestMapping(value="index",method=RequestMethod.GET)publicHashMaphandleRequest(){HashMapmodel=newHashMap();Stringname="HelloWorld";model.put("greeting",name);returnmodel;}下面是我对上述方法的JUnit测试:publicclassControllerTest{p

java - 为什么 junit4 没有 Assert.assertArrayEquals() for double[]s?

在Junit4中似乎有Assert.assertArrayEquals()方法用于除double之外的所有原语,例如Assert.assertArrayEquals(int[]expected,int[]actual)和Assert.assertArrayEquals(char[]expected,char[]actual)但不是Assert.assertArrayEquals(double[]expected,double[]actual,doubleeps)或Assert.assertArrayEquals(double[]expected,double[]actual,doubl

java - JUnit assertEquals 更改字符串

我有一个JUnit测试如下:@TestpublicvoidtestToDatabaseString(){DateConvertorconvertor=newDateConvertor();Datedate=convertor.convert("20/07/1984:00:00:00:00");StringconvertedDate=convertor.toDatabaseString(date);assertEquals("to_date('20/07/1984:00:00:00:00','DD/MM/YYYYHH24:MI:SS')",convertedDate);}测试失败说明:o

java - 在 JUnit 4 的@Before 中获取当前正在执行的 @Test 方法

我想在@Before中获取当前正在执行的测试方法,以便我可以获得应用于当前正在执行的方法的注释。publicclassTestCaseExample{@BeforepublicvoidsetUp(){//getcurrentmethodhere.}@Test@MyAnnotation("id")publicvoidsomeTest{//code}} 最佳答案 尝试TestName规则publicclassTestCaseExample{@RulepublicTestNametestName=newTestName();@Before

java - 如何在 junit 测试中测试 Comparator

我需要测试这个方法-compare()。你能得到建议吗?我能做得多好(所有部分如果,否则-如果,否则)。publicclassAbsFigure{classAreaCompareimplementsComparator{@Overridepublicintcompare(FigureGeneraloneFigure,FigureGeneraltwoFigure){doublefirstValue=oneFigure.area();doublesecondValue=twoFigure.area();intresult=0;if(firstValue>secondValue)result

java - JUnit 崩溃说方法应该是静态的,然后崩溃说它不应该?

我正在尝试在JUnit中做一个简单的示例测试,测试两件事,然后进行拆卸。importorg.junit.*;publicclassTestFoobar{@TestpublicvoidtestOneThing(){//Codethattestsonething}@TestpublicvoidtestAnotherThing(){//Codethattestsanotherthing}@AfterClass@TestpublicvoidtearDownClass()throwsException{//Codeexecutedafterthelasttestmethod}}当我尝试运行它时,

java - 如何使用 SpringJUnit4TestRunner 在 junit 测试中隐藏 spring boot banner.txt?

当我配置SpringBoot应用程序时,我可以通过静态main方法中的运行配置来禁用横幅。到目前为止一切顺利。但是如果我有以下内容怎么办:@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes={MyApplication.class})publicclassMyApplicationTest{....}当我运行这个测试时,它没有使用静态main方法并且显示了横幅,这使得我更难关注相关的日志语句。是否有我可以用来模拟newSpringApplication(MayApplication

java - 如何在 JUnit assertEquals 中强制 BigDecimal 舍入?

每次我运行assertEquals时,我预期的BigDecimal都会四舍五入,这会导致它失败。我如何确保它不会变圆或是否有其他方法?@Testpublicvoidtest(){BigDecimalamount=BigDecimal.valueOf(1000);BigDecimalinterestRate=BigDecimal.valueOf(10);BigDecimalyears=BigDecimal.valueOf(10);InterestCalculatoric=newInterestCalculate(amount,interestRate,years);BigDecimale

java - 如何选择使用 Maven 执行哪些 JUnit5 标签

我刚刚升级了我的解决方案以使用JUnit5。现在尝试为我的测试创建具有两个标签的标签:@Fast和@Slow。首先,我使用下面的Maven条目来配置要使用我的默认构建运行的测试。这意味着当我执行mvntest时,只会执行我的快速测试。我假设我可以使用命令行覆盖它。但是我不知道我会输入什么来运行我的慢速测试....我假设类似......mvntest-Dmaven.IncludeTags=fast,slow这不起作用maven-surefire-plugin2.19.1fastsloworg.junit.jupiterjunit-jupiter-engine5.0.0-M3org.jun

java - Junit中如何处理测试数据?

在TDD(TestDrivenDevelopment)开发过程中,如何处理测试数据?假设一个场景,解析一个日志文件得到需要的列。对于强测试,测试数据如何准备?我将此类文件定位到测试类文件是否合适? 最佳答案 例如,Maven使用文件夹结构约定来处理测试数据:srcmainjava如果你使用maven进行构建,你需要将测试资源放在正确的文件夹中,如果你的构建有不同的东西,你可能想要使用这个结构,因为它不仅仅是一个maven约定,对我来说认为它接近“最佳实践”。 关于java-Junit中如