junit

JUnit mocking with Mockito, EasyMock, etc

蓝咒 提交于 2019-12-22 08:36:33
问题 I'm trying to mock a method of an object inside the class I'm testing. For instance class ClassToTest { public doSomething () { SomeObject a = new SomeObject (); a.doSomethingElse (); } } Is there a way to mock the methods of the variable "a"? I'd like doSomethingElse to do nothing during testing. I'm currently using Mockito but I'm open to any mocking framework. Thanks 回答1: Yes, there is a way, as shown by the following JMockit test: public void testDoSomething(final SomeObject mock) { new

Get user name with Apache CXF 2.4 JAX-RS and Spring Security 3.2

拥有回忆 提交于 2019-12-22 08:32:58
问题 I get the user name in my JAX-RS resource with SecurityContextHolder and that works: @Path("/myresource") public class MyResoure { @Get public String getUserName() { return SecurityContextHolder.getContext().getAuthentication().getName(); } } But I want to inject the SecurityContext into a class field (to write JUnit tests). I tried some documented ways: With javax.ws.rs.core.SecurityContext I get a NullPointerException , because the securityContext is always null . @Path("/myresource")

PowerMockito .when().thenReturn() with randomUUID not returning expected value [duplicate]

做~自己de王妃 提交于 2019-12-22 08:29:33
问题 This question already has answers here : How do I unit test code which uses Java UUID? (4 answers) Closed 2 years ago . I'm trying to test a Web Service method which connects to a SQL Server Database which contains JCR nodes, as we're using JackRabbit. The method looks like: public String addDocumentByJson(String fileName, byte[] fileContent, int status, String userName, String jsonProperties) { UUID id = UUID.randomUUID(); // It does a bunch of operations here return jsonResult; } Where

Display my tests using Eclipse JUnit plugin?

谁说胖子不能爱 提交于 2019-12-22 08:18:31
问题 I have written a test framework for a company-specific language. This test framework is able to output JUnit-like XML. As an example, I can save it to file and open it using Eclipse JUnit view, or disp^lay it in my Hudson server. I want now to have it directly integrated in my Eclipse, in order for these tests to be run on Eclipse "save" action, and for their results to be displayed in a JUnit view. What is the best way to do that ? Save them to file and open that file in Eclipse (and if so,

How do I Unit Test a servlet?

放肆的年华 提交于 2019-12-22 08:06:36
问题 I have a servlet called Calculator . It reads the parameters left , right and op and returns by setting an attribute result in the response. What is the easiest way to unit test this: basically I want to create an HttpServletRequest, set the parameters, and then checking the response - but how do I do that? Here's the servlet code (it's small and silly on purpose): public class Calculator extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { public Calculator() { super();

Android JUnit: Define a different Application subclass

。_饼干妹妹 提交于 2019-12-22 07:01:16
问题 So for my normal Android project, I have the following in AndroidManifest.xml: <application android:name=".utilities.App" ...> .... </application> And then I have my App class: public class App extends Application { .... } And then I have an Android JUnit Test project associated with the Android project. Everything is all fine and dandy and I can write JUnit tests. However, I'm trying to run code coverage with my JUnit tests and I'm getting bloated results. The reason is because my App class

Initialize and clean up per-test data for parallel tests in TestNG

给你一囗甜甜゛ 提交于 2019-12-22 06:42:54
问题 While migrating some tests from JUnit to TestNG, I'm facing an issue because of the difference in how these test frameworks treat their Test class instances. JUnit creates a new instance of the Test class for each test method. So a common pattern I see is: public class MyTest { private Stream inputData; @Before public void setUp() { // Set up some data in (non-static) instance fields // This data is isolated per test inputData = createInputDataStream(); } @Test public void testStuff() { //

Testing和Instrumentation

筅森魡賤 提交于 2019-12-22 05:37:43
Testing和Instrumentation Android提供了一系列强大的测试工具,它针对Android的环境,扩展了业内标准的JUnit测试框架。尽管你可以使用JUnit测试Android工程,但Android工具允许你为应用程序的各个方面进行更为复杂的测试,包括单元层面及框架层面。 Android测试环境的主要特征有: 可以访问Android系统对象。 Instrumentation框架可以控制和测试应用程序。 Android系统常用对象的模拟版本。 运行单个test或test suite的工具,带或不带Instrumentation。 支持以Eclipse的ADT插件和命令行方式管理Test和Test工程。 这篇文章是对Android测试环境和测试方法的简要介绍,并假设你已经拥有一定的Android应用程序编程及JUnit测试的经验。 概要 Android测试环境的核心是一个Instrumentation框架,在这个框架下,你的测试应用程序可以精确控制应用程序。使用Instrumentation,你可以在主程序启动之前,创建模拟的系统对象,如Context;控制应用程序的多个生命周期;发送UI事件给应用程序;在执行期间检查程序状态。Instrumentation框架通过将主程序和测试程序运行在同一个进程来实现这些功能。 通过在测试工程的manifest文件中添加

How to unit test a ResponseBody or ResponseEntity sent by a spring mvc Controller?

淺唱寂寞╮ 提交于 2019-12-22 05:37:10
问题 When I do junit tests, I do something like this to test spring mvc controllers : request.setRequestURI("/projects/"+idProject+"/modify"); ModelAndView mv = handlerAdapter.handle(request, response, controller); where controller tested is like : @RequestMapping(value = "{id}/modify") public String content(ModelMap model, @PathVariable("id") Project object) { But I don't find how to get the ResponseBody answer of request handlers defined like this : @RequestMapping("/management/search") public

How does Mockito timeout work?

▼魔方 西西 提交于 2019-12-22 05:19:16
问题 I'm new to Mockito and JUnit and try to understand basic unit testing with these frameworks. Most concepts in JUnit and Mockito seem straightforward and understandable. However, I got stuck with timeout in Mockito. Does timeout in Mockito play the same role as it does in JUnit? Bellow is my code. @Mock Timeoutable timeoutable; @Test(timeout = 100) public void testJUnitTimeout() { try { Thread.sleep(2000); } catch (InterruptedException ie) { } } @Test public void testMockitoTimeout(){ doAnswer