mockito

How to stub a method call with an implicit matcher in Mockito and Scala

送分小仙女□ 提交于 2021-02-20 05:52:51
问题 My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val divisionNumber:String) Trait AServiceMock mimics the registration logic by using a mocked version of AService trait AServiceMock { def registrationService = { val service = mock[AService] service.registerNewUser(anyString) returns Future(fixedResponse)

How to stub a method call with an implicit matcher in Mockito and Scala

℡╲_俬逩灬. 提交于 2021-02-20 05:52:03
问题 My application code uses AService trait AService { def registerNewUser (username: String)(implicit tenant: Tenant): Future[Response] } to register a new user. Class Tenant is a simple case class: case class Tenant(val vstNumber:String, val divisionNumber:String) Trait AServiceMock mimics the registration logic by using a mocked version of AService trait AServiceMock { def registrationService = { val service = mock[AService] service.registerNewUser(anyString) returns Future(fixedResponse)

Simple unit test for Apache Camel SNMP route

北城余情 提交于 2021-02-20 04:15:09
问题 I'm having some trouble getting a working Camel Spring-Boot unit test written, that tests a simple SNMP route. Here is what I have so far: SnmpRoute.kt open class SnmpRoute(private val snmpProperties: SnmpProperties, private val repository: IPduEventRepository) : RouteBuilder() { @Throws(Exception::class) override fun configure() { logger.debug("Initialising with properties [{}]", snmpProperties) from("snmp:0.0.0.0:1161?protocol=udp&type=TRAP") .process { exchange -> // do stuff } .bean

Spring: unit test for class that has both field & constructor injection

好久不见. 提交于 2021-02-19 02:46:05
问题 I have below setup of classes. class Base { @Autowired private BaseService service; //No getters & setters .... } @Component class Child extends Base { private final SomeOtherService otherService; @Autowired Child(SomeOtherService otherService) { this.otherService = otherService; } } I am writing a unit test for the Child class. If I use @InjectMocks then the otherService comes out to be null. If I use, the constructor of Child class in the setup of the test, then the fields in Base class

Mockito: is it possible to combine mock with a method name to create a methodCall inside a when() call?

落爺英雄遲暮 提交于 2021-02-18 11:15:09
问题 my first question on StackOverflow. I'd like to be able to do something like: SomeClass mock = mock(SomeClass.class); String methodName = "someMethod"; OR Method method = ...someMethod... Both of these things (the mock and the method) would combine to do the following: when(mock.someMethod()).thenReturn(null); Of course, the 'null' value will be changed accordingly for my needs, but I am trying to determine two things: 1) Is it even possible to do something like this in Java? This = combining

How to stub private methods of class under test by Mockito

蓝咒 提交于 2021-02-18 11:01:07
问题 Say we have java class called SomeClass public class SomeClass { private boolean isMethod() { return false; } public void sendRequest(String json, String text) { int messageId; if (isMethod()) { messageId = getMessageId(json); sendMessage(messageId, text); } else { throw new IllegalArgumentException(); } } private void sendMessage(int messageId, String text) { } private int getMessageId(String text) { Pattern p = Pattern.compile("messageId=(\\d+)&"); Matcher m = p.matcher(text); if (m.find())

How to mock persisting and Entity with Mockito and jUnit

半城伤御伤魂 提交于 2021-02-18 01:22:01
问题 I'm trying to find a way to test my entity using Mockito; This is the simple test method: @Mock private EntityManager em; @Test public void persistArticleWithValidArticleSetsArticleId() { Article article = new Article(); em.persist(article); assertThat(article.getId(), is(not(0L))); } How do I best mock the behaviour that the EntityManager changes the Id from 0L to i.e. 1L? Possibly with the least obstructions in readability. Edit: Some extra information; Outside test-scope the EntityManager

How to mock persisting and Entity with Mockito and jUnit

痴心易碎 提交于 2021-02-18 01:17:03
问题 I'm trying to find a way to test my entity using Mockito; This is the simple test method: @Mock private EntityManager em; @Test public void persistArticleWithValidArticleSetsArticleId() { Article article = new Article(); em.persist(article); assertThat(article.getId(), is(not(0L))); } How do I best mock the behaviour that the EntityManager changes the Id from 0L to i.e. 1L? Possibly with the least obstructions in readability. Edit: Some extra information; Outside test-scope the EntityManager

How to test a spring mvc controller

爱⌒轻易说出口 提交于 2021-02-16 15:07:45
问题 I have the following controller: @Controller public class HomeController { @Resource(name="returnGraph") Graph returnGraph; @RequestMapping("/") public String goToHomePage(HttpSession session){ session.setAttribute("sm", returnGraph.getVertexes()); return "home"; } } I tried the following j unit test but it didnt work: public class HomeControllerTest { @Mock Graph returnGraph; @Mock Map<String,Vertex> vertexes; @Mock HttpSession session; HomeController homeController; @Before public void

Junit 5 looking for actual environmental configuration to run the test

旧巷老猫 提交于 2021-02-15 07:37:16
问题 My project is using spring-boot , sping-data-jpa , spring-data-rest , hibernate I have updated to spring boot 2.3.1 . My previous test cases are on Junit 4 , I am migrating to Junit 5 . When I run my test case with junit 5 it is looking for actual environmental configurations like datasource , hibernate:dialect with Junit 4, it was not looking for it. Please find Junit 4 and Junit 5 test class for comparison Junit 4 Test import org.junit.Before; import org.junit.Test; import org.junit.runner