junit4

How to run JUnit tests with Gradle?

那年仲夏 提交于 2019-12-03 18:24:21
问题 Currently I have the following build.gradle file: apply plugin: 'java' sourceSets { main { java { srcDir 'src/model' } } } dependencies { compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar') runtime fileTree(dir: 'libs', include: '*.jar') } This build.gradle file is for my repository here. All of my main files are in src/model/ and their respective tests are in test/model . How do I add a JUnit 4 dependency correctly and then run those tests in the folders of tests/model ? 回答1: How do

Test default value and setter in same test-case or separate test cases

£可爱£侵袭症+ 提交于 2019-12-03 17:58:02
问题 Would you recommend doing any grouping of test cases within @Test methods, or have one @Test method per test scenario? For example, let's suppose that there are different ways to set the context in an application. Is the following idea acceptable? @Test public void testContextSetting() { // Test default setting assert(...) // Test setting a context variable assert(...) ... } Or, would you rather suggest having it like this, having each method as atomic as possible: @Test public void

Spring Boot integration tests doesn't read properties files

喜夏-厌秋 提交于 2019-12-03 16:20:32
I would like to create integration test in which Spring Boot will read a value from .properties file using @Value annotation. But every time I'm running test my assertion fails because Spring is unable to read the value: org.junit.ComparisonFailure: Expected :works! Actual :${test} My test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebTests.ConfigurationClass.class, WebTests.ClassToTest.class}) public class WebTests { @Configuration @ActiveProfiles("test") static class ConfigurationClass {} @Component static class ClassToTest{ @Value("${test}") private String

Java 9 Modules and JUnit 4

北城余情 提交于 2019-12-03 15:34:06
Eclipse oxygen; windows 7; JDK 9 final from 9, 21; JUnit 4.12 and an existing application. As starting point the application can be compiled, executed and all JUnit Tests shows green. Now we use eclipse to generate the file module-info.java. The outcome looks like: module ch.commcity.topsort { exports ch.commcity.topsort; requires junit; } But with the error: junit cannot be resolved to module. The question is: How to tell the file that junit does not define any module and it should be run in compatibility mode? How to tell the file that junit does not define any module and it should be run in

Why are transactions not rolling back when using SpringJUnit4ClassRunner/MySQL/Spring/Hibernate

橙三吉。 提交于 2019-12-03 13:55:41
问题 I am doing unit testing and I expect that all data committed to the MySQL database will be rolled back... but this isn't the case. The data is being committed, even though my log was showing that the rollback was happening. I've been wrestling with this for a couple days so my setup has changed quite a bit, here's my current setup. LoginDAOTest.java: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"file:web/WEB-INF/applicationContext-test.xml", "file:web/WEB-INF

JSR 303 bean validation unit testing with Mockito and Autowiring

ぃ、小莉子 提交于 2019-12-03 13:36:36
问题 I want to junit test my validator class but my validator class has @autowired service classes. How do I inject these dependencies using Mocikto? I am going to call the validator using below line of code. Set<ConstraintViolation<MyDomainPOJOObject>> constraintViolationsFromJavaRules = validator.validate(myDomainPOJOObject, Default.class); Problem is I am not instantiating validator class myself. It's the JSR 303 framework which truely calls the validator's isValid method(). Another thing is I

How do I test exceptions in a parameterized test?

◇◆丶佛笑我妖孽 提交于 2019-12-03 11:39:32
问题 In JUnit4 you can write parameterized unit tests by providing parameters collection in one method, which will be passed to the constructor of the test and testing in another method. If I have a parameter for which I expect an exception to be thrown, how do I specify that? 回答1: if (parameter == EXCEPTION_EXPECTED) { try { method(parameter); fail("didn't throw an exception!"); } catch (ExpectedException ee) { // Test succeded! } } 回答2: this is how i use junit parameterized test with expected

Different teardown for each @Test in jUnit

↘锁芯ラ 提交于 2019-12-03 10:58:16
Is there way to define a different teardown for each @Test in jUnit? Bohemian 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, there is no need for annotations: Simply call it at the end of your test method in a finally:

spring - @ContextConfiguration fail to load config file in src/test/resources

梦想的初衷 提交于 2019-12-03 10:47:35
问题 I've tried to load the spring config file in src/test/resources classpath with the following abstract class: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:/applicationContext.xml"}) public class BaseIntegrationTests { } I have the applicationContext.xml file in src/test/resources but spring cant load it. Thank you. 回答1: To be precise, it's the content of the test output directory ( target/test-classes ) that is on the class path, not src/test/resources .

Maven doesn't find org.junit even though it's in the dependencies

大憨熊 提交于 2019-12-03 10:45:34
问题 I wanted to add a test to my small project (please note I removed some bits from the code & changed package names, so if there's any error you see regarding this it might be not this ;)): package my.pckg; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class SignedRequestCallbackTest { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testCorrectSignedRequest()