junit4

How can I initialize a Spring applicationContext just once for all tests

筅森魡賤 提交于 2019-12-04 20:17:35
I have a set of tests based which need a spring context. For fast test execution I want to make sure that the Spring context is initialized just once, then all the tests should be run against this context, then it should shut down. I already tried the following approaches: Use @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(MyAnnotatedConfig.class) to initialize the spring context Use a @RunWith(SpringJUnit4ClassRunner.class) and @TestExecutionListeners({MyTestExecutionListener.class}) with a handwritten test execution listener that initializes the spring context and injects

Spring MVC + Shiro + Junit Testing

风格不统一 提交于 2019-12-04 18:07:37
Hello I am working on Spring MVC application. Shiro is the security framework for my application. Now I want to write unit tests for my application. I am facing some issues with getting shiro's SecurityUtils class in my controllers while testing. I am pasting my code snippets below. I am re-using my bean definitions for both testing and development. import org.apache.shiro.SecurityUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.nsl.eds.domain.User; @Controller public class

Different teardown for each @Test in jUnit

落爺英雄遲暮 提交于 2019-12-04 17:02:09
问题 Is there way to define a different teardown for each @Test in jUnit? 回答1: Use the @After annotation to indicate the method(s) to be run after every @Test . The full suite of annotations like this are: @BeforeClass - before all @Tests are run @Before - before each @Test is run @After - after each @Test is run @AfterClass - after all @Tests are run I just realised I may not have understood the question. If you are asking how to associate a particular teardown method to a particular @Test method

Database Cleanup after every junit test cases with http calls

為{幸葍}努か 提交于 2019-12-04 15:52:49
I am using junit4 with spring to test my rest web services. For this, I am using HSQL in memory database. To clean the records after every test case, I am removing all the records from tables. But I want to delete only inserted records. I am adding data to database in two places: In Junit test cases. In the rest services. I am making http calls to test the services. Also, I am using same in-memory database in rest services. Kindly help me in removing only inserted records after each test cases. Edited : My concern is deleting the inserted records in http calls to rest services. It is really

struts2 junit 2.3.12 plugin - unable to write successful test in struts2 junit4

倖福魔咒の 提交于 2019-12-04 15:43:44
I'm using struts2 junit 2.3.12 plugin. If I run test directly, then I get : java.lang.NoClassDefFoundError: javax/servlet/ServletContext so I include <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> in my POM, after which I get : java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException so, I remove the above dependency and add : <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>1

Character encoding in IDEA output of AssertionError

牧云@^-^@ 提交于 2019-12-04 08:56:16
I am using IntelliJ IDEA 12.0.4. Have some tests. When i'm running one using JUnit4 framework my Assertion Error looks like: java.lang.AssertionError: Status should be: Черновик expected [true] but found [false] If i am using a TestNG it look like this: java.lang.AssertionError: Status should be: Черновик expected [true] but found [false] All other cyrillic outputs work fine on both framework, only assertion text won't. Project files encoding set to UTF-8. Update: For example simple WebDriver test. I use TestNG and IE. import org.testng.Assert; import org.testng.annotations.AfterSuite;

How to use VisibleForTesting for pure JUnit tests

折月煮酒 提交于 2019-12-04 08:22:17
问题 I´m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTesting clearly without making the thing manually public. Ex: @VisibleForTesting public Address getAddress() { return mAddress; } The method has to be public to let it be "public" to tests, but in that case the annotation doesn't make sense right? why not just use a comment if the annotation will not do nothing? 回答1: According to Android docs: You can optionally specify what the

How do I programmatically run all the JUnit tests in my Java application?

偶尔善良 提交于 2019-12-04 08:08:02
问题 From Eclipse I can easily run all the JUnit tests in my application. I would like to be able to run the tests on target systems from the application jar, without Eclipse (or Ant or Maven or any other development tool). I can see how to run a specific test or suite from the command line. I could manually create a suite listing all the tests in my application, but that seems error prone - I'm sure at some point I'll create a test and forget to add it to the suite. The Eclipse JUnit plugin has a

How to write a junit testcase for a void method that creates a new object

十年热恋 提交于 2019-12-04 06:25:20
问题 public class SupportController{ public void disableUserAccount(String username) throws Exception { UserAccount userAccount = new UserAccount(Constants.SYSTEM, Constants.CONTAINER, username); UserAccount.disableAccount(); } } How would i test that the useraccount created is disabled? 回答1: I would suggest using Mock Objects. Besides that, you can check the JUnit FAQ, where you can find a section about testing methods that return void . Often if a method doesn't return a value, it will have some

Get currently executing @Test method in @Before in JUnit 4

*爱你&永不变心* 提交于 2019-12-04 05:35:06
I want to get currently executing test method in @Before so that I can get the annotation applied on currently executing method. public class TestCaseExample { @Before public void setUp() { // get current method here. } @Test @MyAnnotation("id") public void someTest { // code } } try TestName rule public class TestCaseExample { @Rule public TestName testName = new TestName(); @Before public void setUp() { Method m = TestCaseExample.class.getMethod(testName.getMethodName()); ... } ... Evgeniy pointed to the TestName rule (which i'd never heard of - thanks, Evgeniy!). Rather than using it, i