junit

How to verify (with unit test) that error stack is printed in the log file?

故事扮演 提交于 2020-06-29 03:32:55
问题 In continuing to this answer I wrote a unit test to verify that in case of error, the stack will be printed in the log file. The tested method: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private final Logger logger = LoggerFactory.getLogger(getClass()); public long getFq(String fi) { try { return calcSomeThing(fi.toLowerCase()); } catch (Exception e) { logger.error("unable to calculate SomeThing. Error: " , e); return -1; } } The unit test: import ch.qos.logback.classic.Level;

Mockito and interface event

白昼怎懂夜的黑 提交于 2020-06-27 01:05:09
问题 I am writing an integration test using mockito. The unit under test is connected to a mocked object (objA) through an interface. The functionality that I am trying to mimic happens when the mocked objected fires an event and the unit under test is listening to it. The interface: public interface MyInterfaceAPI{ void fireyMyEvent(String msg); } The unit under test: public class UnitUnderTest{ ObjA objA; public UnitUnderTest(ObjA objA_t) { objA = objA_t; objA.addMyListener(new addMyHandler());

Running unit tests with mpirun using ant

陌路散爱 提交于 2020-06-26 13:57:32
问题 I'm trying to run my unit tests through mpirun using ant. I have specified the task as: <target name="unitTest" depends="buildUnitTest"> <mkdir dir="reports"/> <junit fork="yes" jvm="mpirun java" printsummary="yes" haltonfailure="yes"> <classpath> <pathelement location="./bin"/> <pathelement location="/usr/share/java/junit4.jar"/> </classpath> <jvmarg value="-DDIM=3"/> <jvmarg value="-ea"/> <formatter type="plain"/> <batchtest todir="reports"> <fileset dir="test"> <include name="haparanda

Running unit tests with mpirun using ant

大城市里の小女人 提交于 2020-06-26 13:57:28
问题 I'm trying to run my unit tests through mpirun using ant. I have specified the task as: <target name="unitTest" depends="buildUnitTest"> <mkdir dir="reports"/> <junit fork="yes" jvm="mpirun java" printsummary="yes" haltonfailure="yes"> <classpath> <pathelement location="./bin"/> <pathelement location="/usr/share/java/junit4.jar"/> </classpath> <jvmarg value="-DDIM=3"/> <jvmarg value="-ea"/> <formatter type="plain"/> <batchtest todir="reports"> <fileset dir="test"> <include name="haparanda

Compare JSON response using JUnit and JSONassert

为君一笑 提交于 2020-06-26 06:50:07
问题 I'm relatively new to Java and I'm asking to write test of JSON response server. I found JSONassert very useful but I didn't succeed to write the method getRESTData . Anybody can help please? @Test public void testGetFriends() throws JSONException { JSONObject data = getRESTData("/friends/367.json"); String expected = "{friends:[{id:123,name:\"Corby Page\"}" + ",{id:456,name:\"Solomon Duskis\"}]}"; JSONAssert.assertEquals(expected, data, false); } 回答1: You can get the data as String and pass

How to mock ObjectMapper.readValue() using mockito

非 Y 不嫁゛ 提交于 2020-06-24 22:48:37
问题 I'm testing a service layer and not sure how to mock ObjectMapper().readValue in that class. I'm fairly new to mockito and could figure out how to do it. The following is my code, service.java private configDetail fetchConfigDetail(String configId) throws IOException { final String response = restTemplate.getForObject(config.getUrl(), String.class); return new ObjectMapper().readValue(response, ConfigDetail.class); } ServiceTest.java @Test public void testgetConfigDetailReturnsNull() throws

How to mock ObjectMapper.readValue() using mockito

旧城冷巷雨未停 提交于 2020-06-24 22:48:27
问题 I'm testing a service layer and not sure how to mock ObjectMapper().readValue in that class. I'm fairly new to mockito and could figure out how to do it. The following is my code, service.java private configDetail fetchConfigDetail(String configId) throws IOException { final String response = restTemplate.getForObject(config.getUrl(), String.class); return new ObjectMapper().readValue(response, ConfigDetail.class); } ServiceTest.java @Test public void testgetConfigDetailReturnsNull() throws

Mocking a Keycloak token for testing a Spring controller

穿精又带淫゛_ 提交于 2020-06-24 11:53:05
问题 I want to write unit tests for my spring controller. I'm using keycloak's openid flow to secure my endpoints. In my tests I'm using the @WithMockUser annotation to mock an authenticated user. My problem is that I'm reading the userId from the token of the principal. My unit test now fails because the userId I read from the token is null; if (principal instanceof KeycloakAuthenticationToken) { KeycloakAuthenticationToken authenticationToken = (KeycloakAuthenticationToken) principal;

Mocking a Spy method with Mockito

喜你入骨 提交于 2020-06-24 11:40:06
问题 I am writing a unit test for a FizzConfigurator class that looks like: public class FizzConfigurator { public void doFoo(String msg) { doWidget(msg, Config.ALWAYS); } public void doBar(String msg) { doWidget(msg, Config.NEVER); } public void doBuzz(String msg) { doWidget(msg, Config.SOMETIMES); } public void doWidget(String msg, Config cfg) { // Does a bunch of stuff and hits a database. } } I'd like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn't

Deleting File and Directory in JUnit

。_饼干妹妹 提交于 2020-06-24 09:46:53
问题 I'm writing a test for a method that creates a file in a directory. Here's what my JUnit test looks like: @Before public void setUp(){ objectUnderTest = new ClassUnderTest(); //assign another directory path for testing using powermock WhiteBox.setInternalState(objectUnderTest, "dirPathField", mockDirPathObject); nameOfFile = "name.txt"; textToWrite = "some text"; } @Test public void shouldCreateAFile(){ //create file and write test objectUnderTest.createFile(nameOfFile, textToWrite); /* this