leccles 2 Share Posted January 30 (edited) I previously was involved with another bot, back in the day like 2016 era. I tried my hand at bot scripting during that time but didn't achieve much. I'm a Node, Python, Flutter dev now and have been picking up the Dreambot API and trying my hand at Java which has been quite fun. Anyway, lurking in the forums long enough I've made some progress and particularly enjoy Tree scripts, OSRS is actually down right now for maintenance and I've started refactoring my current mining bot and to verify my node validity and thought it would be sweet to have unit tests. A couple of questions spring to mind; Why is it that a very large portion of the Dreambot API are static? I get most methods are utility methods, but in order to provide any kind of mocking for these in a simulated environment I have to make my own injectable singleton that is then mockable. Going this route also avoids disgusting levels of nesting `try` blocks for each static method that requires mocking. I see a lot of people also referencing "Make your class methods static, so they're callable from anywhere" but something in my brain just refuses to get on board with that, as it kinda goes against a lot of basic principles of programming. Has anyone else had success in developing bots that are actually unit testable. I haven't quite gotten as far as being able to test the `onLoop` of a Node yet but am scouring the web for how to test game loop logic. I know that unit tests alone aren't going to be fool proof in terms of testing bots, but it definitely can help verify changes to the code don't break existing functionality & to also sanity check bot logic. Throwing in a snippet of one of my tests that currently executes the MiningSetup node. If a player doesn't have a pickaxe in their inventory, or their equipped items. This node executes and goes to the closest bank to check there for one, and if found withdraw it. import com.aio_miner.config.Config; import com.aio_miner.data.Pickaxe; import org.dreambot.api.methods.skills.Skill; import org.dreambot.api.utilities.Logger; import org.dreambot.util.EquipmentService; import org.dreambot.util.InventoryService; import org.dreambot.util.SkillsService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.mockito.Mockito; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class MiningSetupTest { private MiningSetup node; private InventoryService inventoryService; private SkillsService skillsService; private EquipmentService equipmentService; private Config config; @BeforeEach void setUp() { inventoryService = mock(InventoryService.class); skillsService = mock(SkillsService.class); equipmentService = mock(EquipmentService.class); config = Config.getConfig(); node = new MiningSetup(config, inventoryService, skillsService, equipmentService); } @Test void playerHasPickaxe() { // Mocks when(inventoryService.contains(Pickaxe.BRONZE_PICKAXE.ID)).thenReturn(true); when(skillsService.getRealLevel(any(Skill.class))).thenReturn(15); when(equipmentService.contains(anyInt())).thenReturn(false); try (MockedStatic<Logger> logMock = Mockito.mockStatic(Logger.class)) { assertTrue(inventoryService.contains(Pickaxe.BRONZE_PICKAXE.ID)); assertFalse(equipmentService.contains(anyInt())); //Node is invalid because we have a pickaxe. assertFalse(node.isValid()); } assertEquals(15, config.getCurrentLevel()); assertEquals(Pickaxe.BRONZE_PICKAXE, config.getBestOwnedPickaxe()); } @Test void playerDoesNotHavePickaxe() { // Mocks when(inventoryService.contains(anyInt())).thenReturn(false); when(skillsService.getRealLevel(any(Skill.class))).thenReturn(0); when(equipmentService.contains(anyInt())).thenReturn(false); try (MockedStatic<Logger> logMock = Mockito.mockStatic(Logger.class)) { assertFalse(inventoryService.contains(Pickaxe.RUNE_PICKAXE.ID)); assertFalse(equipmentService.contains(Pickaxe.RUNE_PICKAXE.ID)); assertTrue(node.isValid()); } assertEquals(0, config.getCurrentLevel()); assertNull(config.getBestOwnedPickaxe()); } } Edited January 30 by leccles Aeglen 1 Link to comment Share on other sites More sharing options...
camelCase 231 Share Posted January 31 obersi 1 Link to comment Share on other sites More sharing options...
Aeglen 294 Share Posted January 31 Your scientists were so preoccupied with whether they could, they didn't stop to think if they should Link to comment Share on other sites More sharing options...
camelCase 231 Share Posted January 31 38 minutes ago, Aeglen said: Your scientists were so preoccupied with whether they could, they didn't stop to think if they should Link to comment Share on other sites More sharing options...
leccles 2 Author Share Posted January 31 Some great feedback, thanks for the thoughtful answers. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now