简体   繁体   中英

spring-boot - running junit test with two servers

I created with java spring-boot a server (“node”). There are a few instance of nodes, as the difference is by a configuration file for every node. For example:

node1.properties:

application.name=FullNode
receiving.server.addresses=tcp://localhost:8001
propagation.server.addresses=tcp://localhost:8002
recovery.server.address=http://localhost:8060
....

node 2.properties:

application.name=FullNode
receiving.server.addresses=tcp://localhost:6001
propagation.server.addresses=tcp://localhost:6002
recovery.server.address=http://localhost:8050

...

To test the process of sending data to the server, I wrote a JUnit test for the TransactionController.

TransactionController:

@RestController
@RequestMapping("/transaction")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;
    ...

    @RequestMapping(method = PUT)
    public ResponseEntity<Response> addTransaction(@Valid @RequestBody 
             AddTransactionRequest addTransactionRequest) {
        return transactionService.addNewTransaction(addTransactionRequest);
    }
...
}

Test:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@SpringBootTest
public class DBTests {
    @Autowired
    private TransactionController transactionController;

    @Test
    public void addTransaction() {
        transactionController.addTransaction(transactionRequest);
    }
}

The problem is, that every node also sends his transactions to the other nodes. But How I could test it with JUnit? I could not just create in the test two instance of a TransactionController, because TransactionController is a spring bean singleton, and the only way to run a node, is with his configuration file.

How could I do it?

I would look at the Mockito framework. Mocking out the TransactionService . Instead of firing up 2 servers which lends itself to be more of an integration test instead of a unit test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM