Tag: unit testing

JUnit – 如何测试具有不同值的方法?

我有一个方法,并希望用不同的值测试它。 我的问题是:如何编写一个JUnit测试,用不同的值测试相同的方法?

Mockito:当调用方法Aa时,执行Bb

我正在使用Mockito进行JUnit测试。 所以从我想要测试的代码中使用给定的A类: class A{ public A(){} public final String a(String x){ return “Aa: ” + x; } } 我想用另一个方法调用替换方法调用Aa与相同的参数和相同类型的返回值。 正如您所看到的,不可能通过扩展类来覆盖方法a,因为它是最终的。 所以我现在拥有的另一个B类方法是Bb: class B{ public B(){} public String b(String x){ return “Bb: ” + x; } } 现在我想确保每次从代码中调用Aa时,都会使用Bb的返回值。 是否有可能通过Mockito实现这一点(类似于Mockito.when(Aa(x)).thenReturn(Bb(x)); )但是使用相同的参数x,却不知道x的值? 任何帮助将不胜感激,谢谢你提前!

Mockitovalidation在我的情况下调用一次函数

我正在使用Mockito来编写我的测试用例。 我有一个简单的类,其中包含一个函数countPerson(boolean) ,我有兴趣测试它: public class School { //School is a singleton class. public void countPerson(boolean includeTeacher) { if (includeTeacher) { countIncludeTeacher(); return; } countOnlyStudents(); } public void countIncludeTeacher() {…} public void countOnlyStudents() {…} } 在我的unit testing中,我想测试countPerson(boolean)函数: public class SchoolTest{ private School mSchool; @Before public void setUp(){ mSchool = School.getInstance(); } @Test public void testCountPerson() { […]

如何使用Mockito和JUnit检查方法中的if语句?

我有方法,我应该测试。 代码(当然有些部分被删除): public class FilterDataController { public static final String DATE_FORMAT = “yyyy-MM-dd”; @Autowired private FilterDataProvider filterDataProvider; @ApiOperation(value = “Get possible filter data”,response = ResponseEntity.class) @ApiResponses(value = { @ApiResponse(…), @ApiResponse(…)}) @RequestMapping(path = “…”, method = RequestMethod.GET) public ResponseEntity getPossibleFilterData( @RequestParam(value = “startDate”) @DateTimeFormat(pattern=DATE_FORMAT) final Date startDate, @RequestParam(value = “endDate”) @DateTimeFormat(pattern=DATE_FORMAT) final Date endDate) { if […]

Gradle和JaCoCo:来自单独子项目的仪器类

我有一个遗留应用程序,它有一个与应用程序模块分开的unit testing模块。 我正在将项目转换为使用Gradle,结构如下所示: /root /module1 /module2 … /moduleN /test 其中测试模块执行module1到moduleN的测试(并依赖于它们)。 我知道这不是一个很好的做法,因为它有点挫败了unit testing的目的,但众所周知,遗留代码总是令人头疼。 所以在我开始重构代码之前,每个模块都有自己的unit testing(这意味着以合理的方式拆卸测试模块,即大量工作)我想找到一个临时解决方案来获得正确的代码覆盖率,即,让JaCoCo设备来自module1,…,moduleN的所有类,而不仅仅是模块测试。 有没有办法告诉JaCoCo从其他模块中检测类?

Spock框架:间谍问题

我在Spock中使用Spy有一个问题,它要么不能正常工作,要么我的理解是错误的,所以我试图弄清楚这一点。 考虑这段代码(Java): public class CallingClass { public String functionOne() { //does stuff return “one”; } public String functionTwo() { String one = functionOne(); return “some string ” + one; } } 现在我想测试一下这个事实,即functionTwo调用functionOne以及定义functionOne的返回值(想象一下,如果functionOne真的很复杂,我不想在我的测试中执行它只是想将它存根并将其设置为返回一定的值)。 为此,我在Groovy中编写以下测试(使用Spock): class CallingClassTest extends Specification { def “check that functionTwo calls functionOne”() { def c = Spy(CallingClass) c.functionOne() >> “mocked function return” when: […]

元素在XPath Checker中找到,但在Selenium中找不到

我有以下XPath: //div[contains(@id, ‘box’)]/div/h4/small/a[contains(@href, ‘google’)]/@href 当我在XPath Checker(Firefox扩展)中试用这个XPath时,它始终可以正常运行。 但是当我在Selenium中执行以下操作时: System.out.println(selenium.getAttribute(“//div[contains(@id, ‘box’)]/div/h4/small/a[contains(@href, ‘google’)]/@href”)); 它一直给我以下日志错误: 14:30:56.311 INFO – Got result: OK on session 5a1401d374a04779bbe6f7fe9a0b4536 14:30:56.315 INFO – Command request: getAttribute[//div[contains(@id, ‘box’)]/div/h4/small/a[contains(@href, ‘google’)]/@href, ] on session 5a1401d374a04779bbe6f7fe9a0b4536 14:30:56.372 INFO – Got result: ERROR: Element //div[contains(@id, ‘box’)]/div/h4/small/a[contains(@href, ‘google’)]/ not found on session 5a1401d374a04779bbe6f7fe9a0b4536 我疯了解决这个问题。 有没有人在我的代码行中看到任何错误?

logback.xml的perf4j设置

大家好,我想知道在使用perf4j时logback.xml的确切配置设置。 我在logback.xml中创建此配置 firstBlock,secondBlock <!– The NotificationThresholds param configures the sending of JMX notifications when statistic values exceed specified thresholds. This config states that the firstBlock max value should be between 0 and 800ms, and the secondBlock max value should be less than 1500 ms. You can also set thresholds on the Min, Mean, StdDev, Count […]

使用Jersey Rest测试框架和Mockito进行unit testing

有人可以帮我这个吗? 我正在使用Jersey rest测试框架版本2.21为Rest资源编写unit testing。(在Grizzly容器上)。 当我调试测试类时,我看到myManager的模拟对象。 但是当调试进入我的“MyResouce类”时,myManager对象变为null并获得NullPointerexception。 尝试过不同的人提供的解决方案,但没有运气。有人可以帮助我。 我差不多三天就遇到了这个问题。 🙁 我的资源类是这样的。 @Component @Path(“/somepath”) public class MyResource { @Autowired private MyManager myManager; @Path(“/somepath”) @GET @Produces(“application/json”) @ResponseType(String.class) public Response getResults(@QueryParam(“queryParam”) String number) { // myManager is an interface String str = myManager.getResult(number); } } 这是我的测试类 public class MyResourceTest extends JerseyTest { @Mock private MyManager myManager; @InjectMocks private MyResource […]

如何对使用Java UUID的代码进行unit testing?

我有一段代码,希望用Java UUID( UUID.randomUUID() )填充响应对象的一个​​属性。 如何从外部对此代码进行unit testing以检查此行为? 我不知道将在其中生成的UUID。 需要测试的示例代码: // To test whether x attribute was set using an UUID // instead of hardcode value in the response class A { String x; String y; } // Method to test public A doSomething() { // Does something A a = new A(); a.setX( UUID.randomUUID()); return a; […]