junit4

Issue with @Transactional annotations in Spring JPA

只谈情不闲聊 提交于 2019-12-06 15:57:44
I have a doubt related to transactions within transactions. For background, I have a School entity object which has Set of Students entity object mapped to it. I am using Spring Data JPA which is taking care of all the crud operations. I have a SchoolManagementService class which has @Transactional(readonly=true) set at the class level and for all updating methods I am using @Transactional over them. In my SchoolManagementService class I have a method deleteStudents(List) which I have marked as @Transactional. In this method I am calling StudentsRepository.delete(studentId) again and again. I

How to add arguments to junit test triggered from command line?

这一生的挚爱 提交于 2019-12-06 10:21:53
This thread here shows how to test a JAR with JUnit tests. However, I want to submit additional properties from the command line (because I want to test my main method). How to run JUnit tests for Java from the command line e.g. java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [MyTest] [PortNumber] [fileToRead] Anything like that possible? You could use system properties java -cp ... YOUR_TEST -DportNumber=1234 Then in code, you can retrieve it with System.getProperty("portNumber") 来源: https://stackoverflow.com/questions/19529849/how-to-add-arguments-to-junit-test-triggered-from

Spring MVC + Shiro + Junit Testing

雨燕双飞 提交于 2019-12-06 10:21:18
问题 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

Use of parameterized tests with JUnit

末鹿安然 提交于 2019-12-06 09:24:58
问题 I have a class where one of the public methods is a perfect fit for parameterized tests. Also I am reading that you usually keep a correspondence between testcases (a class having multiple methods with @Test annotated methods) and classes in the project. Is it possible somehow to use both Parameterized.class and JUnitCore.class as the runner ? Right now if utilizing Parameterized I can't work out how to setup non-parameterized testing of methods within the same testcase. I have thought about

Spring 3.0 junit test DispatcherServlet

烈酒焚心 提交于 2019-12-06 06:09:47
问题 I am trying to test my application with junit. Therefore I've setup the following class: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/META-INF/spring/applicationContext-test.xml" ) @TransactionConfiguration @Transactional public class DispatcherServletTest extends AbstractJUnit4SpringContextTests { private MockHttpServletRequest request; private MockHttpServletResponse response; private DispatcherServlet dispatcher; @Before public void setUp() throws Exception {

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

流过昼夜 提交于 2019-12-06 01:59:57
问题 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 } } 回答1: try TestName rule public class TestCaseExample { @Rule public TestName testName = new TestName(); @Before public void setUp() { Method m = TestCaseExample.class.getMethod(testName.getMethodName()); ... } ... 回答2

Gradle Android unit tests that depend on multiple module

╄→гoц情女王★ 提交于 2019-12-06 01:36:21
I am working on a project which has multiple modules. We are writing junit test cases for application and we are using below exception. Test running started java.lang.RuntimeException: java.lang.ClassNotFoundException: Below is the project dependency  Library project 1  Library project 2 – depends on Library project 1  Library project 3– depends on Library project 1  Main launch project– depends on Library project 2 and Library project 3 From Library project 1 we are accessing the classes of Library project 2 using factory pattern to get the object. When we are writing the test cases for

SpringJUnit4ClassRunner initialize beans for each test?

扶醉桌前 提交于 2019-12-05 22:17:09
The following test illustrates that this test bean is initialized twice by Spring. I'm hoping someone can tell me why this is so, since it should only be once. Here's the test: import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.InitializingBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {} ) public class TestAfterPropsSet implements InitializingBean {

mvc controller test with session attribute

依然范特西╮ 提交于 2019-12-05 21:13:05
I'm trying to test a method with this signature: @Autowired HttpSession http_Session; @RequestMapping(method=RequestMethod.GET, value="/search/findByName") public @ResponseBody List<Map> search(@RequestParam(value="name", required=true) String name){ Integer user_id = http_Session.getAttribute("userinfo"); } userinfo is a class which contains informations about the user and set in session scope when the user logged in.but when I try the test : @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { "classpath:/META-INF/applicationContext.xml"}) public

transactions not working with Spring 3.1 – H2 – junit 4– hibernate 3.2

老子叫甜甜 提交于 2019-12-05 19:48:09
I have the following test.. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/schedule-agents-config-context.xml"}) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) @Transactional public class H2TransactionNotWorkingTest extends SubmitAgentIntegratedTestBase { private static final int FIVE_SUBMISSIONS = 5; @Autowired private ApplicationSubmissionInfoDao submissionDao; private FakeApplicationSubmissionInfoRepository fakeRepo; @Before public void setUp() { fakeRepo = fakeRepoThatNeverFails(submissionDao, null);